java基础(7Day-抽象类 & 接口-静态代理)

抽象类 & 接口-静态代理

抽象

抽象类

public class Emp{}
	抽象类:abstract (abs - tr act)
public abstract class Emp{
	//抽象方法无需写代码,目的要求继承者,必须重写这个方法
	public abstract String show();
}

抽象类-抽象方法(继承者必须重写)

在这里插入图片描述

抽象类的第二个特点,不能直接 new

在这里插入图片描述

抽象类的继承

肯定要重写(实现)抽象方法

在这里插入图片描述

注意super(…)调用父类对应的构造方法,必须放到第一行

在这里插入图片描述

通过抽象类,我们明白子类,父类的方法被调用,取决于 new

在这里插入图片描述

案例

public abstract class Emp {
	private String name;
	private double salary;
	private double bonus;
	
	public abstract String show();
	
	public Emp() {
	}
	public Emp(String name, double salary) {
		this.name = name;
		this.salary = salary;
	}
	public String getName() {
		return name;
	}
	public double getSalary() {
		return salary;
	}
	public double getBonus() {
		return bonus;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	public void setBonus(double bonus) {
		this.bonus = bonus;
	}
	@Override
	public String toString() {
		return "Emp [name=" + name + ", salary=" + salary + ", bonus=" + bonus + "]";
	}
}
public class Worker extends Emp {
	private int num;
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public Worker() {}
	
	public Worker(String name,double salary,int num) {
		super(name,salary);//调用Emp的构造方法,这条语句必须在第一行
		this.num=num;
	}
	
	@Override
	public String show() {
		this.setBonus(this.num*3);
		return this.getName()+"\t基本工资:"+this.getSalary()+"\t奖金:"+this.getBonus()+"\t件数:"+num+"\t总工资:"+(this.getSalary()+this.getBonus());
	}
}
public class Salesman extends Emp {
	private double saleamt;
	private double payment;
	public double getPayment() {
		return payment;
	}
	public double getSaleamt() {
		return saleamt;
	}
	public void setPayment(double payment) {
		this.payment = payment;
	}
	public void setSaleamt(double saleamt) {
		this.saleamt = saleamt;
	}
	public Salesman() {}
	public Salesman(String name,double salary,double saleamt,double payment){
		super(name, salary);
		this.saleamt=saleamt;
		this.payment=payment;
	}
	@Override
	public String show() {
		this.setBonus((saleamt*0.04)-((saleamt-payment)*0.1));
		return this.getName()+"\t基本工资:"+this.getSalary()+"\t销售额:"+saleamt+"\t回款额:"+payment+"\t奖金:"+this.getBonus()+"\t总工资:"+(this.getSalary()+this.getBonus());
	}
}
public class DemoEmp {
	public static void main(String[] args) {
		Emp w1 = new Worker("程咬金", 10000, 500);
		System.out.println(w1.show());
		Emp s1 = new Salesman("李世民", 10000, 500000, 400000);
		System.out.println(s1.show());
	}
}

接口

1.接口没有成员变量
2.接口只有方法,常量
3.接口方法和抽象方法一样没有代码
4.接口和抽象类一样不能 new
5.一个类可以继承多个接口(干爹),但是一个类只能有一个父类(亲爹)
6.它也是一种 数据类型
在两个类多个类没有明显继承关系(学生是人),这时候找干爹(接口)
//动物类
public class Animal{
	public void cry(){...}
}
public class Person extends Animal{
	
}
public class Dog extends Animal{
	
}

案例

public interface ICry {
	public void cry();
}
public interface ILaugh {
	public void laugh();
}
public class Dog extends Object implements ICry,ILaugh {
	private String name;
	private String type;//品种
	private int age;
	@Override
	public void laugh() {
		System.out.println("狗"+this.name+"笑了");
	}
	@Override
	public void cry() {
		System.out.println("狗"+this.name+"流泪了");
	}
	public Dog() {
		
	}
	public Dog(String name, String type, int age) {
		this.name = name;
		this.type = type;
		this.age = age;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "Dog [name=" + name + ", type=" + type + ", age=" + age + "]";
	}
}
public class Student extends Object implements ICry,ILaugh {
	private String name;
	private String jy;//专业
	private int age;
	@Override
	public void laugh() {
		System.out.println("学生"+this.name+"笑了");
	}
	@Override
	public void cry() {
		System.out.println("学生"+this.name+"哭了");
	}
	public Student() {
		
	}
	public Student(String name, String jy, int age) {
		this.name = name;
		this.jy = jy;
		this.age = age;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getJy() {
		return jy;
	}
	public void setJy(String jy) {
		this.jy = jy;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "Student [name=" + name + ", jy=" + jy + ", age=" + age + "]";
	}
}
public class Demo {
	public static void main(String[] args) {
		ICry []arr = new ICry[2];
		arr[0]=new Student("程咬金", "砍人", 90);
		arr[1]=new Dog("小二黑", "土狗", 2);
		arr[0].cry();
		arr[1].cry();
		
		ILaugh []ar = new ILaugh[2];
		ar[0]=new Student("程咬金", "砍人", 90);
		ar[1]=new Dog("小二黑", "土狗", 2);
		ar[0].laugh();
		ar[1].laugh();
	}
}

接口和静态代理(AOP)

Spring IOC - 反射注解 MAP
AOP 接口 代理
中介
买房子
1.买房子——》卖房子
2.买房子 ——》 中介 ——》 卖房子
public interface IBuy {
	public void buyHouse();//买房子
}
public class Teacher implements IBuy {
	
	private String name;
	public Teacher() {}
	public Teacher(String name) {
		this.name=name;
	}
	
	@Override
	public void buyHouse() {
		System.out.println(this.name+"交钱");
		System.out.println(this.name+"拿到钥匙");
	}
	
}
public class Agent implements IBuy {
	private IBuy teacher;
	
	public Agent(IBuy tearcher) {
		this.teacher=tearcher;
	}
	
	@Override
	public void buyHouse() {
		System.out.println("》》》中介正在找房子》》》");
		System.out.println("》》》中介协商签合同》》》");
		teacher.buyHouse();
		System.out.println("》》》中介收取3%的手续费");
	}
	
}
public class Demo {
	public static void main(String[] args) {
		IBuy t = new Teacher("老师");
		IBuy a = new Agent(t);
		a.buyHouse();
	}
}

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值