修正class PrintChan extends Threadt private char pchars private int times; public Printchar(char c, int t) f pchar times public void run() for(int i=e;iktimes,it+) if(i%1e ==e) system.out.print(n);system.out.print(pchar+· public class ThreadTest ( public static yoid main(string[) ares) Printchar pe - new Printchan('c',10e); pc.start();
时间: 2023-11-21 21:03:54 浏览: 135
我发现这段代码存在一些语法错误,修正后的代码如下:
```
class PrintChar extends Thread {
private char pChar;
private int times;
public PrintChar(char c, int t) {
pChar = c;
times = t;
}
public void run() {
for (int i = 0; i < times; i++) {
if (i % 10 == 0) {
System.out.print("\n");
}
System.out.print(pChar + " ");
}
}
}
public class ThreadTest {
public static void main(String[] args) {
PrintChar pc = new PrintChar('c', 100);
pc.start();
}
}
```
这个程序的功能是在控制台上输出一定数量的字符 'c',每行 10 个字符,共输出 100 个字符。其中涉及到了多线程的概念,通过继承 Thread 类并重写 run 方法来实现多线程。
相关问题
class Person{ private String name; private int age; private char gender; public Person(String name,int age,char gender){ this.name=name; this.age=age; this.gender=gender; } public void printInfo(){ System.out.println("姓名: "+name); System.out.println("年龄: "+age); System.out.println("性别: "+gender); } } class Student extends Person{ private String school; private int score; public Student(String name,int age,char gender,String school,int score){ super(name,age,gender); this.school=school; this.score=score; } public void printInfo(){ super.printInfo(); System.out.println("学校: "+school); System.out.println("成绩: "+score); } } class Teacher extends Person{ private String course; public Teacher(String name,int age,char gender,String course){ super(name,age,gender); this.course=course; } public void printInfo(){ super.printInfo(); System.out.println("课程: "+course); } } //替换下面的学生信息,修改主类的名字 public class Experiment_04_01 { public static void main(String[] args) { Student stud=new Student01("lili",19,'F',"齐鲁工业大学",90); Teacher tech=new Teacher("coco",30,'F',"高级程序设计"); stud.printInfo(); System.out.println(); tech.printInfo(); } }
抱歉,您的代码有一些错误。首先,您在创建学生对象时使用了错误的类名,应该是"Student"而不是"Student01"。其次,由于您的代码中没有定义名为"Student01"的类,因此需要将主类的名字从"Experiment_04_01"修改为"Main"。以下是修改后的代码:
```java
class Person {
private String name;
private int age;
private char gender;
public Person(String name, int age, char gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public void printInfo() {
System.out.println("姓名: " + name);
System.out.println("年龄: " + age);
System.out.println("性别: " + gender);
}
}
class Student extends Person {
private String school;
private int score;
public Student(String name, int age, char gender, String school, int score) {
super(name, age, gender);
this.school = school;
this.score = score;
}
public void printInfo() {
super.printInfo();
System.out.println("学校: " + school);
System.out.println("成绩: " + score);
}
}
class Teacher extends Person {
private String course;
public Teacher(String name, int age, char gender, String course) {
super(name, age, gender);
this.course = course;
}
public void printInfo() {
super.printInfo();
System.out.println("课程: " + course);
}
}
public class Main {
public static void main(String[] args) {
Student stud = new Student("lili", 19, 'F', "齐鲁工业大学", 90);
Teacher tech = new Teacher("coco", 30, 'F', "高级程序设计");
stud.printInfo();
System.out.println();
tech.printInfo();
}
}
```
请注意,我已经将主类名更改为"Main",并将学生对象的类名更正为"Student"。现在您可以运行该代码并得到预期的输出。
java创建以下三个类,使得子类继承父类,重写类中run方法,并分别创建teacher教师对象和student学生对象,调用run方法输出对应语句。 Human(父类)--成员变量:name、age、sex;方法:void run,输出“跑步让我快乐。”Teacher(子类)--成员变量:addr(地址);方法:void run,输出“跑步让我健康”。 Student(子类)--成员变量:classname(班级);方法:void run,输出“跑步让我年轻”。
好的,以下是创建三个类的代码:
```java
// Human类
public class Human {
private String name; // 姓名
private int age; // 年龄
private char sex; // 性别
public Human(String name, int age, char sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public void run() {
System.out.println("跑步让我快乐。");
}
}
// Teacher类
public class Teacher extends Human {
private String addr; // 地址
public Teacher(String name, int age, char sex, String addr) {
super(name, age, sex);
this.addr = addr;
}
@Override
public void run() {
System.out.println("跑步让我健康。");
}
}
// Student类
public class Student extends Human {
private String classname; // 班级
public Student(String name, int age, char sex, String classname) {
super(name, age, sex);
this.classname = classname;
}
@Override
public void run() {
System.out.println("跑步让我年轻。");
}
}
```
在这个代码中,我们定义了三个类`Human`、`Teacher`和`Student`,其中`Teacher`和`Student`类都继承自`Human`类。在`Human`类中,我们定义了三个成员变量`name`、`age`、`sex`,并且定义了一个`run`方法,输出“跑步让我快乐。”在`Teacher`类和`Student`类中,我们分别定义了`addr`和`classname`成员变量,并且重写了`run`方法,分别输出“跑步让我健康。”和“跑步让我年轻。”在创建`Teacher`和`Student`对象时,我们通过调用`super`关键字来调用父类的构造方法,并将`name`、`age`、`sex`作为参数传递给父类的构造方法。这样就可以创建`Teacher`和`Student`对象,并调用它们的`run`方法,输出对应语句。
阅读全文