class SuperClass{ String name; public SuperClass(String s)/{ name =s; System. out.println(s); public void method(){ System. out.println("superclass!"); } }
时间: 2023-04-06 11:01:19 浏览: 75
这是一个 Java 代码段,其中定义了一个名为 SuperClass 的类,它有一个字符串类型的属性 name 和一个构造函数,构造函数的参数为字符串类型 s,构造函数会将 s 赋值给属性 name 并输出 s。此外,SuperClass 还有一个名为 method 的方法,它会输出 "superclass!"。
相关问题
帮我用中文注释一下代码:// ThreadTester.java // Multiple threads printing at different intervals. public class ThreadTester { public static void main( String [] args ) { // create and name each thread PrintThread thread1 = new PrintThread( "thread1" ); PrintThread thread2 = new PrintThread( "thread2" ); PrintThread thread3 = new PrintThread( "thread3" ); System.err.println( "主线程将要启动三个线程" ); thread1.start(); // start thread1 and place it in ready state thread2.start(); // start thread2 and place it in ready state thread3.start(); // start thread3 and place it in ready state System.err.println( "三个线程启动完成, 主线程运行结束\n" ); } // end main } // end class ThreadTester // class PrintThread controls thread execution class PrintThread extends Thread { private int sleepTime; // assign name to thread by calling superclass constructor public PrintThread( String name ) { super( name ); // pick random sleep time between 0 and 5 seconds sleepTime = ( int ) ( Math.random() * 5001 ); } // method run is the code to be executed by new thread public void run() { // put thread to sleep for sleepTime amount of time try { System.err.println( getName() + " 进入睡眠状态,睡眠时间是: " + sleepTime ); Thread.sleep( sleepTime ); } // if thread interrupted during sleep, print stack trace catch ( InterruptedException exception ) { } // print thread name System.err.println( getName() + " 睡眠醒来" ); } // end method run } // end class PrintThread
// ThreadTester.java
// 多个线程在不同时间间隔打印。
public class ThreadTester {
public static void main(String[] args) {
// 创建并为每个线程命名
PrintThread thread1 = new PrintThread("线程1");
PrintThread thread2 = new PrintThread("线程2");
PrintThread thread3 = new PrintThread("线程3");
System.err.println("主线程将要启动三个线程");
// 启动线程1、2、3,并将它们放在就绪状态
thread1.start();
thread2.start();
thread3.start();
System.err.println("三个线程启动完成,主线程运行结束\n");
}
}
// 类PrintThread控制线程执行
class PrintThread extends Thread {
private int sleepTime; // 睡眠时间
// 调用父类构造函数为线程分配名称
public PrintThread(String name) {
super(name);
// 随机分配睡眠时间(0~5秒)
sleepTime = (int)(Math.random() * 5001);
}
// run方法是新线程要执行的代码
public void run() {
// 让线程睡眠sleepTime时间
try {
System.err.println(getName() + " 进入睡眠状态,睡眠时间是: " + sleepTime);
Thread.sleep(sleepTime);
}
// 如果在睡眠期间线程被中断,就打印堆栈跟踪信息
catch (InterruptedException exception) {}
// 打印线程名称
System.err.println(getName() + "睡眠醒来");
}
}
class SuperClass { private int n; SuperClass(){ System.out.println("SuperClass()"); } SuperClass(int n) { System.out.println("SuperClass(int n)"); this.n = n; } } // SubClass 类继承 class SubClass extends SuperClass{ private int n; SubClass(){ // 自动调用父类的无参数构造器 System.out.println("SubClass"); } public SubClass(int n){ super(300); // 调用父类中带有参数的构造器 System.out.println("SubClass(int n):"+n); this.n = n; } } // SubClass2 类继承 class SubClass2 extends SuperClass{ private int n; SubClass2(){ super(300); // 调用父类中带有参数的构造器 System.out.println("SubClass2"); } public SubClass2(int n){ // 自动调用父类的无参数构造器 System.out.println("SubClass2(int n):"+n); this.n = n; } } public class TestSuperSub{ public static void main (String args[]){ System.out.println("------SubClass 类继承------"); SubClass sc1 = new SubClass(); SubClass sc2 = new SubClass(100); System.out.println("------SubClass2 类继承------"); SubClass2 sc3 = new SubClass2(); SubClass2 sc4 = new SubClass2(200); } }
这段代码演示了Java中的继承和构造器的调用顺序。
其中,`SuperClass`是一个父类,有两个构造器,一个无参数构造器和一个带有一个参数的构造器。`SubClass`和`SubClass2`是两个子类,它们分别继承`SuperClass`。
在`SubClass`和`SubClass2`的构造器中,分别调用了`super()`方法来调用父类的构造器。
当创建`SubClass`对象时,会先自动调用父类的无参数构造器,然后再调用子类的无参数构造器。当创建`SubClass`对象时,会先调用父类的带有一个参数的构造器,然后再调用子类的带有一个参数的构造器。
因此,运行这段代码的结果如下:
```
------SubClass 类继承------
SuperClass()
SubClass
SuperClass(int n)
SubClass(int n):100
------SubClass2 类继承------
SuperClass(int n)
SubClass2
SuperClass()
SubClass2(int n):200
```
可以看到,当创建`SubClass`对象时,先输出了`SuperClass()`,然后输出了`SubClass`。当创建`SubClass2`对象时,先输出了`SuperClass(int n)`,然后输出了`SubClass2`。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)