分析以下代码package experiment05; public class Test { public static void main(String[] args) { Person person = new Person(); PersonAction input = new PersonInput(); PersonAction display = new PersonDisplay(); input.process(person); display.process(person); } }
时间: 2023-06-23 18:08:12 浏览: 107
这段代码定义了一个名为Test的类,包含一个名为main的静态方法。在main方法中,首先创建了一个名为person的Person对象。然后,创建了两个PersonAction类型的对象,分别为input和display。这里使用了多态性,因为PersonInput和PersonDisplay都是PersonAction类型的子类。
接下来,调用input对象的process方法,该方法接受一个Person类型的参数person,并将其作为参数传递。在PersonInput类中,process方法会从控制台读取输入并设置Person对象的属性。然后,调用display对象的process方法,该方法同样接受一个Person类型的参数person,并将其作为参数传递。在PersonDisplay类中,process方法会将Person对象的属性输出到控制台。
因此,该程序的作用是从控制台读取输入并将其设置为Person对象的属性,然后将Person对象的属性输出到控制台。
相关问题
为什么以下代码构件JButton,JLabel不显示package com.experiment; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * 使用JDialog对话框显示问候语 */ @SuppressWarnings("all") public class demo14 { public static void main(String[] args) { JFrame frame = new JFrame("问候"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setSize(500, 300); frame.setLocationRelativeTo(null); JLabel label = new JLabel("请输入您的姓名:"); JTextField jTextField = new JTextField(15); JPanel panel = new JPanel(); frame.setContentPane(panel); JButton button = new JButton("确定"); panel.add(label); panel.add(jTextField); panel.add(button); } }
这是因为您没有将按钮和标签添加到 JFrame 窗口中。需要使用 frame.add() 方法将它们添加到窗口中。同时,需要将 setVisible() 方法放在最后,因为只有在添加了所有组件后,才应该将窗口显示出来。以下是修改后的代码:
package com.experiment;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* 使用JDialog对话框显示问候语
*/
@SuppressWarnings("all")
public class demo14 {
public static void main(String[] args) {
JFrame frame = new JFrame("问候");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
JLabel label = new JLabel("请输入您的姓名:");
JTextField jTextField = new JTextField(15);
JPanel panel = new JPanel();
JButton button = new JButton("确定");
panel.add(label);
panel.add(jTextField);
panel.add(button);
frame.add(panel);
frame.setVisible(true);
}
}
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"。现在您可以运行该代码并得到预期的输出。
阅读全文
相关推荐
















