上转型对象和接口回调

 //上转型对象的理解

class AA
{
	void func()
	{
		System.out.println("class AA");
	}
}

class BB extends AA
{
	void func()//重写方法
	{
		System.out.println("class BB");
	}
}

class CC extends AA
{
	void func()//重写方法
	{
		System.out.println("class CC");
	}
}

public class test7
{
	public static void main(String args[])
	{
		AA a;
		a = new AA();
		a.func();
		
		a = new BB();//a是BB对象的上转型对象
		a.func();
		
		a = new CC();//a是CC对象上的上转型对象
		a.func();
	}
}

输出:

class AA
class BB
class CC

PS:上转型对象一般都在抽象类的时候使用

####################################################################################################################################

接口回调:

把使用某一接口的类创建的对象的引用赋给该接口声明的接口变量中,那么该接口变量就可以调用被类实现的接口中的方法,当接口变量调用被类实现的接口中的方法时,就是通知相应的对象调用接口的方法,这一过程称为接口的回调。

//接口回调的理解

interface I
{
	void hello();//接口在声明方法时,可以省略方法前面的public和abstract
}

class AB implements I
{
	public void hello()//但在类里实现接口的方法时,一定要有public来修饰
	{
		System.out.println("Hell world !");
	}
}

class CD implements I
{
	public void hello()//但在类里实现接口的方法时,一定要有public来修饰
	{
		System.out.println("Hello Java !");
	}
}

public class test8
{
	public static void main(String args[])
	{
		I i;//接口变量i中存放对象的引用
		i = new AB();
		i.hello();//接口回调
		
		i = new CD();
		i.hello();//接口回调
	}
}

输出:

Hell world !
Hello Java !

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个较为复杂的项目,需要综合运用Java的面向对象编程思想和相关知识。我可以提供一个基本的代码框架,供您参考。 首先,我们需要定义一个基本的员工类,包含一些基本的属性和方法: ``` public abstract class Employee { protected String name; protected int age; protected String gender; protected double salary; public Employee(String name, int age, String gender, double salary) { this.name = name; this.age = age; this.gender = gender; this.salary = salary; } public abstract double getSalary(); public abstract void setSalary(double salary); public abstract void display(); } ``` 这里我们使用了抽象类来定义基本的员工类,其中包含了一些基本的属性和方法,并且定义了抽象方法 getSalary() 和 setSalary(),用于获取和设置员工的薪水,由子类实现。 接下来,我们定义几个具体的员工类,继承自基本的员工类,同时实现抽象方法: ``` public class Manager extends Employee { private double bonus; public Manager(String name, int age, String gender, double salary, double bonus) { super(name, age, gender, salary); this.bonus = bonus; } @Override public double getSalary() { return salary + bonus; } @Override public void setSalary(double salary) { this.salary = salary; } @Override public void display() { System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Gender: " + gender); System.out.println("Salary: " + getSalary()); System.out.println("Position: Manager"); } } public class Programmer extends Employee { private int workingHours; public Programmer(String name, int age, String gender, double salary, int workingHours) { super(name, age, gender, salary); this.workingHours = workingHours; } @Override public double getSalary() { return salary + 100 * workingHours; } @Override public void setSalary(double salary) { this.salary = salary; } @Override public void display() { System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Gender: " + gender); System.out.println("Salary: " + getSalary()); System.out.println("Position: Programmer"); } } ``` 这里我们定义了两个具体的员工类,一个是经理,一个是程序员,它们分别继承了基本的员工类,并实现了抽象方法。其中,经理类有一个额外的属性 bonus,表示奖金;程序员类有一个额外的属性 workingHours,表示工作时间。 接下来,我们定义一个接口,用于实现增删改查等操作: ``` public interface EmployeeManagement { public void add(Employee employee); public void delete(String name); public void update(String name, double salary); public Employee search(String name); } ``` 这里我们定义了一个接口 EmployeeManagement,包含了增删改查等方法。 然后,我们定义一个具体的类,实现这个接口: ``` import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class EmployeeManagementImpl implements EmployeeManagement { private List<Employee> employees = new ArrayList<>(); @Override public void add(Employee employee) { employees.add(employee); } @Override public void delete(String name) { Iterator<Employee> iterator = employees.iterator(); while (iterator.hasNext()) { Employee employee = iterator.next(); if (employee.name.equals(name)) { iterator.remove(); break; } } } @Override public void update(String name, double salary) { for (Employee employee : employees) { if (employee.name.equals(name)) { employee.setSalary(salary); break; } } } @Override public Employee search(String name) { for (Employee employee : employees) { if (employee.name.equals(name)) { return employee; } } return null; } public void displayAll() { for (Employee employee : employees) { employee.display(); System.out.println(); } } } ``` 这里我们定义了一个具体的类 EmployeeManagementImpl,实现了 EmployeeManagement 接口,同时使用了 List 来存储员工信息。 最后,我们可以定义一个界面类,用于展示各种操作: ``` import java.util.Scanner; public class EmployeeManagementUI { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); EmployeeManagementImpl employeeManagement = new EmployeeManagementImpl(); while (true) { System.out.println("Please select an operation:"); System.out.println("1. Add an employee"); System.out.println("2. Delete an employee"); System.out.println("3. Update an employee's salary"); System.out.println("4. Search for an employee"); System.out.println("5. Display all employees"); System.out.println("0. Exit"); int choice = scanner.nextInt(); switch (choice) { case 1: System.out.println("Please enter the employee's name:"); String name = scanner.next(); System.out.println("Please enter the employee's age:"); int age = scanner.nextInt(); System.out.println("Please enter the employee's gender:"); String gender = scanner.next(); System.out.println("Please enter the employee's salary:"); double salary = scanner.nextDouble(); System.out.println("Please select the employee's position:"); System.out.println("1. Manager"); System.out.println("2. Programmer"); int position = scanner.nextInt(); if (position == 1) { System.out.println("Please enter the manager's bonus:"); double bonus = scanner.nextDouble(); Manager manager = new Manager(name, age, gender, salary, bonus); employeeManagement.add(manager); } else if (position == 2) { System.out.println("Please enter the programmer's working hours:"); int workingHours = scanner.nextInt(); Programmer programmer = new Programmer(name, age, gender, salary, workingHours); employeeManagement.add(programmer); } break; case 2: System.out.println("Please enter the employee's name:"); String deleteName = scanner.next(); employeeManagement.delete(deleteName); break; case 3: System.out.println("Please enter the employee's name:"); String updateName = scanner.next(); System.out.println("Please enter the employee's new salary:"); double newSalary = scanner.nextDouble(); employeeManagement.update(updateName, newSalary); break; case 4: System.out.println("Please enter the employee's name:"); String searchName = scanner.next(); Employee employee = employeeManagement.search(searchName); if (employee != null) { employee.display(); } else { System.out.println("Employee not found!"); } break; case 5: employeeManagement.displayAll(); break; case 0: System.exit(0); default: System.out.println("Invalid choice!"); break; } } } } ``` 这里我们定义了一个界面类 EmployeeManagementUI,用于展示各种操作。在这个类中,我们首先创建了一个 EmployeeManagementImpl 对象,然后使用 Scanner 来读取用户输入的选项,根据用户选择的选项来执行不同的操作。 以上是一个基本的企业人事信息管理系统的代码框架,实现了增删改查等基本功能,并且考虑了面向抽象和接口编程、开闭原则、子类继承、接口实现、上转型对象接口回调、异常处理、输入输出流、文件保存、界面设计等方面。您可以根据实际需求,进行相应的修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值