java定义一个学校的职工类
抽象出教师、学生和后勤人员的公共特性和方法,使用继承定义教师、学生和后勤人员等新类,每个类都又区别于其他类的特殊属性,构造对应的方法分别计算出教师、学生和后勤人员的人数,...
抽象出教师、学生和后勤人员的公共特性和方法,使用继承定义教师、学生和后勤人员等新类,每个类都又区别于其他类的特殊属性,构造对应的方法分别计算出教师、学生和后勤人员的人数,计算教师、学生和后勤人员的平均年龄
展开
1个回答
展开全部
import java.util.*;
public class Test1
{
public static void main(String[] args)
{
Student s1 = new Student(15);
Student s2 = new Student(12);
Student s3 = new Student(15);
Student s4 = new Student(13);
Student s5 = new Student(11);
Teacher t1 = new Teacher(35);
Teacher t2 = new Teacher(30);
Worker w1 = new Worker(45);
Worker w2 = new Worker(43);
Worker w3 = new Worker(42);
List<People> students = new ArrayList<People>();
List<People> teachers = new ArrayList<People>();
List<People> workers = new ArrayList<People>();
students.add(s1);
students.add(s2);
students.add(s3);
students.add(s4);
students.add(s5);
teachers.add(t1);
teachers.add(t2);
workers.add(w1);
workers.add(w2);
workers.add(w3);
Test1 t = new Test1();
s1.sayHello();
t.display(students);
t1.sayHello();
t.display(teachers);
w1.sayHello();
t.display(workers);
}
public void display(List<People> as)
{
int Count = 0;
int sum = 0;
for(People p:as)
{
sum = sum + p.age;
Count++;
}
System.out.println("共有人数" + Count);
System.out.println("平均年龄" + (sum/Count));
}
}
abstract class People
{
int age; //年龄
public People(int age)
{
this.age = age;
}
public abstract void sayHello();
public void displayAvgAge()
{
System.out.println("年龄:" + age);
}
}
class Student extends People
{
public Student(int age)
{
super(age);//调用抽象类的构造方法构造
}
public void sayHello()
{
System.out.println("I'm a student!");
}
}
class Teacher extends People
{
public Teacher(int age)
{
super(age);//调用抽象类的构造方法构造
}
public void sayHello()
{
System.out.println("I'm a teacher!");
}
}
class Worker extends People
{
public Worker(int age)
{
super(age);//调用抽象类的构造方法构造
}
public void sayHello()
{
System.out.println("I'm a worker!");
}
}
总结一下,主要就是写了一个abstract类,里面有个abstract方法。当学生老师职工分别实现的时候要重写这个方法。然后算那个平均年龄的叫我十分匪夷所思。加到类里面不合适,先用ArrayList(集合框架,灰常好用)来搞一个吧。运行完全正常。楼主建立一个Test1.java文件,然后编译一下,用虚拟机跑一下就可以看见结果。
共同的属性就是age,共同的方法就是getAvgAge(),每个类特殊的方法就是sayHello();
public class Test1
{
public static void main(String[] args)
{
Student s1 = new Student(15);
Student s2 = new Student(12);
Student s3 = new Student(15);
Student s4 = new Student(13);
Student s5 = new Student(11);
Teacher t1 = new Teacher(35);
Teacher t2 = new Teacher(30);
Worker w1 = new Worker(45);
Worker w2 = new Worker(43);
Worker w3 = new Worker(42);
List<People> students = new ArrayList<People>();
List<People> teachers = new ArrayList<People>();
List<People> workers = new ArrayList<People>();
students.add(s1);
students.add(s2);
students.add(s3);
students.add(s4);
students.add(s5);
teachers.add(t1);
teachers.add(t2);
workers.add(w1);
workers.add(w2);
workers.add(w3);
Test1 t = new Test1();
s1.sayHello();
t.display(students);
t1.sayHello();
t.display(teachers);
w1.sayHello();
t.display(workers);
}
public void display(List<People> as)
{
int Count = 0;
int sum = 0;
for(People p:as)
{
sum = sum + p.age;
Count++;
}
System.out.println("共有人数" + Count);
System.out.println("平均年龄" + (sum/Count));
}
}
abstract class People
{
int age; //年龄
public People(int age)
{
this.age = age;
}
public abstract void sayHello();
public void displayAvgAge()
{
System.out.println("年龄:" + age);
}
}
class Student extends People
{
public Student(int age)
{
super(age);//调用抽象类的构造方法构造
}
public void sayHello()
{
System.out.println("I'm a student!");
}
}
class Teacher extends People
{
public Teacher(int age)
{
super(age);//调用抽象类的构造方法构造
}
public void sayHello()
{
System.out.println("I'm a teacher!");
}
}
class Worker extends People
{
public Worker(int age)
{
super(age);//调用抽象类的构造方法构造
}
public void sayHello()
{
System.out.println("I'm a worker!");
}
}
总结一下,主要就是写了一个abstract类,里面有个abstract方法。当学生老师职工分别实现的时候要重写这个方法。然后算那个平均年龄的叫我十分匪夷所思。加到类里面不合适,先用ArrayList(集合框架,灰常好用)来搞一个吧。运行完全正常。楼主建立一个Test1.java文件,然后编译一下,用虚拟机跑一下就可以看见结果。
共同的属性就是age,共同的方法就是getAvgAge(),每个类特殊的方法就是sayHello();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询