用面向对象的设计思想实现教师和学生的信息输出
首先设计和实现父类People,Teacher和Student分别是他的子类,编写测试主类,实现教师和学生的信息输出
求大神帮忙 !! 急 在线等!!!
用面向对象的设计思想实现教师和学生的信息输出
答案:2 悬赏:30
解决时间 2021-11-28 20:16
- 提问者网友:时间却是纷扰
- 2021-11-28 12:48
最佳答案
- 二级知识专家网友:冷眼_看世界
- 2021-11-28 14:25
package com.baidu.zhidao;
class People {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void action() {
System.out.println(this.getName() + ":" + this.getAge() + "岁");
System.out.println("人类的行为");
}
}
class Teacher extends People {
public void action() {
System.out.println(this.getName() + ":" + this.getAge() + "岁");
System.out.println("老师的行为");
}
Teacher(String name, int age) {
// TODO Auto-generated constructor stub
this.setAge(age);
this.setName(name);
}
}
class Student extends People {
Student(String name, int age) {
// TODO Auto-generated constructor stub
this.setAge(age);
this.setName(name);
}
public void action() {
System.out.println(this.getName() + ":" + this.getAge() + "岁");
System.out.println("学生的行为");
}
}
public class Main {
public static void main(String[] args) {
People st = new Student("小名", 22);
st.action();
People th = new Teacher("老师", 44);
th.action();
}
}
class People {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void action() {
System.out.println(this.getName() + ":" + this.getAge() + "岁");
System.out.println("人类的行为");
}
}
class Teacher extends People {
public void action() {
System.out.println(this.getName() + ":" + this.getAge() + "岁");
System.out.println("老师的行为");
}
Teacher(String name, int age) {
// TODO Auto-generated constructor stub
this.setAge(age);
this.setName(name);
}
}
class Student extends People {
Student(String name, int age) {
// TODO Auto-generated constructor stub
this.setAge(age);
this.setName(name);
}
public void action() {
System.out.println(this.getName() + ":" + this.getAge() + "岁");
System.out.println("学生的行为");
}
}
public class Main {
public static void main(String[] args) {
People st = new Student("小名", 22);
st.action();
People th = new Teacher("老师", 44);
th.action();
}
}
全部回答
- 1楼网友:我叫很个性
- 2021-11-28 15:30
没看懂什么意思?
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯