找到 34423 篇文章 关于编程
12K+ 浏览量
可以使用 import 语句以及字符 * 导入包中的所有类。例如 - 可以使用 import java.util.*; 导入 java.util 包中的所有类。以下给出了一个在 Java 中演示此功能的程序:示例 实时演示import java.util.*; public class Demo { public static void main(String args[]) { Stack stack = new Stack(); stack.push("Apple"); stack.push("Mango"); stack.push("Guava"); stack.push("Pear"); stack.push("Orange"); System.out.println("The stack elements are: " + stack); } }输出栈元素 ... 阅读更多
78K+ 浏览量
接口像类一样包含变量和方法,但与类不同,接口中的方法默认是抽象的。如果一个类实现了多个接口,或者如果一个接口本身扩展了多个接口,就会发生接口的多重继承。以下给出了一个在 Java 中演示接口多重继承的程序:示例 实时演示interface AnimalEat { void eat(); } interface AnimalTravel { void travel(); } class Animal implements AnimalEat, AnimalTravel { public void eat() { System.out.println("Animal is eating"); } public void travel() { System.out.println("Animal is travelling"); ... 阅读更多
243 浏览量
可以使用 interface 关键字定义接口。它像类一样包含变量和方法,但与类不同,接口中的方法默认是抽象的。接口主要用于实现抽象,并且不能被实例化。以下给出了一个在 Java 中演示接口的程序:示例 实时演示interface AnimalSound { abstract void sound(); } class CatSound implements AnimalSound { public void sound() { System.out.println("Cat Sound: Meow"); } } class DogSound implements AnimalSound { public void sound() { System.out.println("Dog Sound: Bark"); } } ... 阅读更多
792 浏览量
如果一个类包含至少一个抽象方法,则该类为抽象类。它也可以包含其他非抽象方法。可以使用 abstract 关键字将类声明为抽象类。此外,抽象类不能被实例化。以下给出了一个在 Java 中演示抽象类的程序:示例 实时演示abstract class Animal { abstract void sound(); } class Cat extends Animal { void sound() { System.out.println("Cat Meows"); } } class Dog extends Animal { void sound() { System.out.println("Dog Barks"); } } class Cow extends Animal ... 阅读更多
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 访问修饰符指定,则可以从同一包或不同包中的子类访问它们。关键字 protected 用于指定此修饰符。以下给出了一个在 Java 中演示 protected 访问修饰符的程序:示例 实时演示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 访问修饰符指定,则只能从其声明的类中访问它们。关键字 private 用于指定此修饰符。以下给出了一个在 Java 中演示 private 访问修饰符的程序:示例 实时演示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(); } ... 阅读更多
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP