今天学了什么
用Person类设计Student类和Teacher类
Person类:
package person;
public class Person {
protected String name,sex;
protected int age;
public void setname(String name) {
this.name=name;
}
public void setsex(String sex) {
this.sex=sex;
}
public void setage(int age) {
this.age=age;
}
public String getname() {
return name;
}
public String getsex() {
return sex;
}
public int getage() {
return age;
}
}
Student类:
package person;
public class Student extends Person {
private String no,classno;
private double score;
public void setno(String no) {
this.no=no;
}
public String getno() {
return no;
}
public void setclassno(String classno) {
this.classno=classno;
}
public String getclassno() {
return classno;
}
public void setscore(double score) {
this.score=score;
}
public double getscore() {
return score;
}
}
Teacher类:
package person;
public class Teacher extends Person {
private String tno,object;
private double wages;
public void settno(String tno) {
this.tno=tno;
}
public String gettno() {
return tno;
}
public void setobject(String object) {
this.object=object;
}
public String getobject() {
return object;
}
public void setwages(double wages) {
this.wages=wages;
}
public double getwages() {
return wages;
}
}
Show类:
package person;
public class Show {
public static void main(String[] args) {
Student s = new Student();
s.setname("小明");
s.setsex("男");
s.setage(19);
s.setno("20190001");
s.setclassno("1905");
s.setscore(86);
System.out.println("-----学生-----");
System.out.println("姓名:"+s.getname());
System.out.println("性别:"+s.getsex());
System.out.println("年龄:"+s.getage());
System.out.println("学号:"+s.getno());
System.out.println("班级:"+s.getclassno());
System.out.println("成绩:"+s.getscore());
System.out.println(" ");
Teacher t = new Teacher();
t.setname("张三");
t.setsex("女");
t.setage(46);
t.settno("0006");
t.setobject("java");
t.setwages(8000);
System.out.println("-----教师-----");
System.out.println("姓名:"+t.getname());
System.out.println("性别:"+t.getsex());
System.out.println("年龄:"+t.getage());
System.out.println("工号:"+t.gettno());
System.out.println("科目:"+t.getobject());
System.out.println("工资:"+t.getwages());
}
}
运行截图:
遇到的问题
无
明天计划
练习面向对象题目