class Teacher extends Person{//定义一个老师类继承person类
String workplace;
int salary ;
static int num=0;
static int avsalary=0;
static int allsalary=0;
Teacher(){}//无参的构造方法
Teacher(String workplace, int salary){
this.workplace=workplace;
this.salary=salary;
num++;
allsalary+=salary;
avsalary=allsalary/num;
}
public void show(){
System.out.println("教师总人数是:"+num);
System.out.println("教师的平均工资为:"+avsalary);
System.out.println("教师的总工资为:"+allsalary);
}
}
class Person{
private String name;
private int age;
public Person(){};
public Person(String name,int age){
this.name = name;
this.age = age;
}
public void show(){}
public boolean equals(Object obj){
Person per =(Person) obj;//强制转换
if (name==per.name&&age==per.age){//进行判断对象是否相等
return true;
}
else return false;
}
public String toString() {//toString方法是基类object类所拥有的,所以可以进行重写
return "姓名为:"+name +"年龄为"+age;
}
}
class Student extends Person{
String workplace;
int score ;
static int num;
static int avsacore;
static int allscore;
public Student(){}
Student(String workplace,int score){
this.workplace=workplace;
this. score= score;
num++;
allscore+=score;
avsacore=allscore/num;
}
public boolean equals(Object obj){
Person per =(Person) obj;
if (super.equals(obj)==true){
return true;
}
else return false;
}
public void show(){9
System.out.println("学生总人数是:"+num);
System.out.println("学生的的总分数为:"+allscore);
System.out.println("学生的平均分为:"+avsacore);
}
public String toString() {
return "姓名为:"+name +"年龄为"+age;
}
}
public class Main{
public static void main(String[] args) {
Student a1 = new Student("学校1",80);
Student a2 = new Student("学校2",100);
Student a3 = new Student("学校3",90);
Teacher b1 = new Teacher("清华大学",50000);
Teacher b2 = new Teacher("清华大学",30000);
Person p1 = new Person("韩鹏程",200);
a3.show();
b2.show();
System.out.println(p1);//如果直接输出对象的话,toString方法也会被调用
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)