实验三 Java类的继承与派生

第1关 类的继承

package step1;

import java.util.Scanner;

class Person {
    /********** Begin **********/
    // 自行设计类的实现
    //姓名
    private String name;
    //性别
    private String sex;
    //年龄
    private int age;

    //构造方法
    public Person(String name, String sex, int age){
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    //重写toString()方法
    public String toString(){
        return name + "," + sex + "," + age;
    }
	/********** End **********/	                                                     
}

class Student extends Person {
    /********** Begin **********/
    // 自行设计类的实现
    //学号
    private String no;
    //入学时间
    private String enter;
    //专业
    private String major;

    //构造方法
    public Student(String name, String sex, int age, String no, String enter, String major){
        super(name, sex, age);
        this.no = no;
        this.enter = enter;
        this.major = major;
    }

    //重写toString()方法
    public String toString(){
        return super.toString() + "," + no + "," + enter + "," + major;
    }
	/********** End **********/	                                                     
}

class Teacher extends Person {
    /********** Begin **********/
    // 自行设计类的实现
    //职称
    private String pro;
    //部门
    private String department;

    //构造方法
    public Teacher(String name, String sex, int age, String pro, String department){
        super(name, sex, age);
        this.pro = pro;
        this.department = department;
    }

    //重写toString()方法
    public String toString(){
        return super.toString() + "," + pro + "," + department;
    }
	/********** End **********/		                                                     
}

public class Lab3_1 {
	public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
		Student student = new Student(sc.next(), sc.next(), sc.nextInt(), sc.next(), sc.next(), sc.next());
		Teacher teacher = new Teacher(sc.next(), sc.next(), sc.nextInt(), sc.next(), sc.next());
		System.out.println("学生基本信息为:" + student);
		System.out.println("教师的信息为:" + teacher);
		sc.close();	                                                     
	}
}	

第2关 上转型对象的使用,多态的特性

package step2;

abstract class Employee {
	public abstract double earnings();
}
class YearWorker extends Employee {
	//重写earnings()方法
    /********** Begin **********/
    public double earnings() {
        return 100000;
    }
	/********** End **********/	                                               		
}
class MonthWorker extends Employee {
	//重写earnings()方法
    /********** Begin **********/
    public double earnings() {
        return 10000 * 12;
    }
	/********** End **********/	                                               		
}
class WeekWorker extends Employee {
	//重写earnings()方法
    /********** Begin **********/
    public double earnings() {
        return 5000 * 4 * 12;
    }
	/********** End **********/	                                                		
}
class Company {
	Employee[] employees;
	double salaries = 0;
	Company(Employee[] employees) {
		this.employees = employees;
	}
	public double salariesPay() {
		salaries = 0;
		//计算salaries
		/********** Begin **********/
        for (Employee employee : employees) {
            salaries += employee.earnings();
            }
	    /********** End **********/	                             		
		return salaries;
	}	
}
public class HardWork {
	public static void main(String[] args) {
		Employee[] employees = new Employee[20];
		for (int i = 0; i < employees.length; i++) {
			if(i%3==0)
				employees[i] = new WeekWorker();
			else if(i%3==1)
				employees[i] = new MonthWorker();
			else if(i%3==2)
				employees[i] = new YearWorker();
		}
		Company company = new Company(employees);
		System.out.println("公司年工资总额:" + company.salariesPay());
	}
}

第3关 接口的使用

package step3;

// 自行设计接口和实现类
/********** Begin **********/
// 定义接口
interface Display {
    public void display();
}

// 通知类
class Inform implements Display {
    // 实现 display() 方法
    public void display() {
        System.out.println("通知内容");
    }
}

// 汽车类
class Car implements Display {
    // 实现 display() 方法
    public void display() {
        System.out.println("汽车油量");
    }
}

// 广告类
class Adervise implements Display {
    // 实现 display() 方法
    public void display() {
        System.out.println("广告消息");
    }
}
/********** End **********/	                                                     

public class Lab3_3 {
	public static void main(String[] args) {
		Display[] arr = { new Inform(), new Car(), new Adervise() };
		for (Display d : arr) {
			d.display();
		}
	}
}

第4关 综合应用

Student
package step4;

public class Student {
    /********** Begin **********/
    private String sno;  // 学号
    private String sname;  // 姓名
    private String sdept;  // 系别

    public String getSno() {
        return sno;
    }

    public void setSno(String sno) {
        this.sno = sno;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSdept() {
        return sdept;
    }

    public void setSdept(String sdept) {
        this.sdept = sdept;
    }

    @Override
    public String toString() {
        return "学号: " + sno + "	姓名: " + sname + "	系部: " + sdept;
    }
    /********** End **********/
}
IStudentDAO
 package step4;
 
 public interface IStudentDAO {
	/********** Begin **********/
    void insertStudent(Student student);  // 插入学生信息
    void updateStudent(Student student);  // 更新学生信息
    void deleteStudent(String sno);  // 删除学生信息
    Student findStudentBySno(String sno);  // 根据学号查找学生信息
    void displayAllStudent();  // 显示所有学生信息
    /********** End **********/

}
StudentDAOImpl
package step4;

import java.util.ArrayList;
import java.util.List;

public class StudentDAOImpl implements IStudentDAO {

	static List<Student> students = new ArrayList<Student>();
	
	/********** Begin **********/
    //自行实现所有其它方法
    @Override
    public void insertStudent(Student student) {
        students.add(student);
    }

    @Override
    public void updateStudent(Student student) {
        for (Student stu : students) {
            if (stu.getSno().equals(student.getSno())) {
                stu.setSname(student.getSname());
                stu.setSdept(student.getSdept());
                break;
            }
        }
    }

    @Override
    public void deleteStudent(String sno) {
        for (Student stu : students) {
            if (stu.getSno().equals(sno)) {
                students.remove(stu);
                break;
            }
        }
    }

    @Override
    public Student findStudentBySno(String sno) {
        for (Student stu : students) {
            if (stu.getSno().equals(sno)) {
                return stu;
            }
        }
        return null;
    }
    /********** End **********/

    /**
    * 显示所有学生信息记录参考代码
    */
    @Override
    public void displayAllStudent() {
        if (students.size() > 0) {
            for (Student stu : students) {
                System.out.println(stu);
            }
        }else {
            System.out.println("数据库中无学生记录!");
        }
    }

}
MainClass
package step4;

import java.util.Scanner;

public class MainClass {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		StudentDAOImpl studentDAOImpl = new StudentDAOImpl();
		//1. 插入学生信息
		Student stu = new Student();
		stu.setSno(scanner.next());
		stu.setSname(scanner.next());
		stu.setSdept(scanner.next());
		studentDAOImpl.insertStudent(stu);
		//2. 显示插入学生信息
		System.out.println("1. 插入学生信息如下:");
		studentDAOImpl.displayAllStudent();
		//3. 更新学生信息
		stu.setSname("李四");
		stu.setSdept("计算机系");
		studentDAOImpl.updateStudent(stu);
		System.out.println("2. 更新后学生信息如下:");
		System.out.println(studentDAOImpl.findStudentBySno(stu.getSno()));
		//4. 删除指定学生信息
		System.out.println("3. 删除当前学号学生信息:" + stu.getSno());
		studentDAOImpl.deleteStudent(stu.getSno());
		System.out.println("学生信息已删除!");
		//2. 显示插入学生信息
		System.out.println("5. 显示所有学生信息:");
		studentDAOImpl.displayAllStudent();

		scanner.close();
	}
}

热门相关:地球第一剑   无限杀路   无限杀路   最强反套路系统   修仙界最后的单纯