找到 4330 篇文章 适用于 Java 8
4K+ 次查看
可以使用 final 关键字将类设为 final。final 类不能被继承,因此 final 关键字通常用于类以防止继承。以下给出一个演示 Java 中 final 类的程序:示例 实时演示final class A { private int a = 15; public void printA() { System.out.println("Value of a = " + a); } } public class Demo { public static void main(String args[]) { A obj = new A(); obj.printA(); } }输出Value of a = ... 阅读更多
73 次查看
量词之一是加号(+)。它匹配用序列指定的子序列的一个或多个。以下给出一个演示在 Java 中使用量词加号(+) 查找匹配项的程序:示例 实时演示import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String args[]) { Pattern p = Pattern.compile("o+"); Matcher m = p.matcher("o oo ooo"); System.out.println("The input string is: o oo ooo"); System.out.println("The Regex is: o+ "); System.out.println(); while (m.find()) { System.out.println("Match: ... 阅读更多
105 次查看
find() 方法查找输入序列中与所需模式匹配的多个子序列。此方法在 Matcher 类中可用,该类在 java.util.regex 包中可用以下给出一个在 Java 中使用 find() 方法查找多个子序列的程序:示例 实时演示import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String args[]) { Pattern p = Pattern.compile("the"); Matcher m = p.matcher("eclipses of the sun and the moon"); System.out.println("Subsequence: the"); System.out.println("Sequence: eclipses of the sun and the moon"); ... 阅读更多
882 次查看
final 关键字用于类以创建 final 类。final 类不能被继承,因此 final 关键字通常用于类以防止继承。以下给出一个演示 Java 中 final 类的程序:示例 实时演示final class A { private int a = 9; public void printA() { System.out.println("Value of a = " + a); } } public class Demo { public static void main(String args[]) { A obj = new A(); obj.printA(); } }输出Value of ... 阅读更多
201 次查看
如果类的数据成员和方法使用受保护的访问修饰符指定,则可以从同一包或不同包中的子类访问它们。关键字 protected 用于指定此修饰符。以下给出一个演示 Java 中受保护的访问修饰符的程序:示例 实时演示class A { protected int a = 9; public void printA() { System.out.println("Value of a = " + a); } } public class Demo { public static void main(String args[]) { A obj = new A(); ... 阅读更多
137 次查看
如果类的数据成员和方法使用私有访问修饰符指定,则只能从其声明的类中访问它们。关键字 private 用于指定此修饰符。以下给出一个演示 Java 中私有访问修饰符的程序:示例 实时演示class A { private int a = 6; public void printA() { System.out.println("Value of a = " + a); } } public class Demo { public static void main(String args[]) { A obj = new A(); obj.printA(); } ... 阅读更多
654 次查看
可以使用递归计算数字的幂。这里数字是 x,它被提高到 n 次幂。以下给出一个演示此方法的程序:示例 实时演示public class Demo { static double pow(double x, int n) { if (n != 0) return (x * pow(x, n - 1)); else return 1; } public static void main(String[] args) { System.out.println("7 to the power 3 is " + pow(7, 3)); System.out.println("4 to the power 1 ... 阅读更多
1K+ 次查看
可以使用 java.util.regex.Pattern.matches() 方法验证邮政编码。此方法匹配邮政编码的正则表达式和给定的输入邮政编码,如果它们匹配则返回 true,否则返回 false。以下给出一个演示此方法的程序:示例 实时演示public class Demo { public static void main(String args[]) { String zipCode = "83592-1537"; String regex = "\d{5}(-\d{4})?"; System.out.println("The zip code is: " + zipCode); System.out.println("Is the above zip code valid? " + zipCode.matches(regex)); } }输出The zip code is: 83592-1537 Is the above ... 阅读更多
3K+ 次查看
可以使用 java.util.regex.Pattern.matches() 方法验证电话号码。此方法匹配电话号码的正则表达式和给定的输入电话号码,如果它们匹配则返回 true,否则返回 false。注意:我们为示例考虑了一个演示号码,因为我们无法公开使用我们的电话号码。以下给出一个演示此方法的程序:示例 实时演示public class Demo { public static void main(String args[]) { String phoneNumber = "9999999998"; String regex = "(0/91)?[7-9][0-9]{9}"; System.out.println("The phone number is: " + phoneNumber); System.out.println("Is the ... 阅读更多
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP