接口、设计模式
一、面向对象练习
做题四大原则:
一、继承链:自己没有找父
A
|
B
/ \
C D
二、 编译看类型、确定方法,运行找对象
三、就近最优原则
四、父类引用对子类新增方法不可见
public class Test {
public static void main(String[] args) {
A a1=new A(); // A and A,A and D
//多态调用
A a2=new B(); // B and A,A and D
B b =new B(); // B and B,B and A,A and D
C c=new C();
D d =new D();
System.out.println(a1.show(b)); //A and A
System.out.println(a1.show(c)); //A and A
System.out.println(a1.show(d)); //A and D
System.out.println(a2.show(b)); //B and A
System.out.println(a2.show(c)); //B and A
System.out.println(a2.show(d)); //A and D
System.out.println(b.show(b)); //B and B
System.out.println(b.show(c)); //B and B
System.out.println(b.show(d)); //A and D
}
}
class A{
public String show(D obj){
return ("A and D");
}
public String show(A obj){
return ("A and A");
}
}
class B extends A{
//新增方法
public String show(B obj){
return ("B and B");
}
//重写方法
@Override
public String show(A obj){
return ("B and A");
}
}
class C extends B{}
class D extends B{}
二、接口
1、介绍
特殊的抽象类,是一个引用数据类型
功能的集合(抽象方法的集合)
PS:类只能单继承,接口可以多实现
接口的优点:
更加灵活
便于后期维护
接口实现解耦(高内聚低耦合)
定义开发规范
PS:当父类和接口都可以使用时,推荐使用接口,但是接口不可以完全取代父类
2、使用
1、在jdk7及以前版本,接口只可以写以下内容:
公共静态常量: public static final(默认可以省略)
公共抽象方法:public abstract(默认可以省略)
2、在jdk8及以后版本新增以下内容:
静态方法: 只能通过接口名.方法名调用
默认方法: 被default修饰的方法
只能通过实现类的重写方法调用
3、注意事项:
1.不能实例化
2.接口需要被实现 通过implements关键字进行实现
3.根据实现类对象使用
具体实现类 : 重写所有的抽象方法 + 按需新增
抽象实现类 : 按需重写抽象方法 + 按需新增
4.类只能单继承类,类可以多实现接口,需要重写多个抽象方法
5.类与接口之间只能是类实现接口,可以多实现
6.接口与接口之间,可以多继承
7.一个类需要先继承后实现
三、设计模式
1、单例模式
1、实现方式:饿汉式 懒汉式
2、设计步骤:
1、私有的构造器
2、私有静态的当前类引用
3、公共静态的访问方式
饿汉式:
class Hungry{
private Hungry(){}
private static Hungry h=new Hungry();
public static Hungry getHungry(){
return h;
}
}
懒汉式:
class Lazy{
private Lazy(){}
private static Lazy l;
public static Lazy getLazy(){
if(l==null) l=new Lazy();
return l;
}
}
2、代理模式
目前主要讲静态代理
组成:
真实角色
代理角色
条件:
真实角色与代理角色要求实现相同的接口
代理角色需要持有真实角色的引用-->通过属性来维护
代理行为
好处:减少与真实角色的互动,便于后期维护
public class Agency {
public static void main(String[] args) {
Manager manager=new Manager();
Hr hr=new Hr(manager);
hr.add();
}
}
interface Add{
void add();
}
class Manager implements Add{
public void add(){
System.out.println("录用");
}
}
class Hr implements Add{
Manager manager;
public Hr(Manager manager) {
this.manager = manager;
}
public void add(){
System.out.println("发布招聘信息");
System.out.println("预约面试");
manager.add();
System.out.println("面试成功");
System.out.println("谈薪资");
}
}
3、工厂模式
组成:
抽象产品角色:接口|父类
具体产品角色:实现类
工厂角色
import java.util.Scanner;
public class PizzaTest{
public static void main(String[] args) {
PizzaFactory.MakePizza();
}
}
class PizzaFactory{
public static void MakePizza(){
Scanner sc = new Scanner(System.in);
System.out.print("请选择要制作的披萨(1.培根披萨 2.海鲜披萨):");
int i = sc.nextInt();
switch (i) {
case 1:
System.out.print("请输入培根克数:");
int a = sc.nextInt();
System.out.print("请输入披萨大小:");
int b = sc.nextInt();
System.out.print("请输入披萨价格:");
int c = sc.nextInt();
Pizza1 p1=new Pizza1(a,b,c);
p1.show();
break;
case 2:
System.out.print("请输入配料信息:");
String str = sc.next();
System.out.print("请输入披萨大小:");
int m = sc.nextInt();
System.out.print("请输入披萨价格:");
int n = sc.nextInt();
Pizza2 p2=new Pizza2(m,n,str);
p2.show();
break;
default:
System.out.println("没有此类披萨");
}
}
}
abstract class Pizza{
private int price;
private int size;
public Pizza(){}
public Pizza(int price, int size) {
this.price = price;
this.size = size;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
abstract void show();
}
class Pizza1 extends Pizza{
private String name="培根披萨";
private int weight;
public Pizza1(){}
public Pizza1(int price, int size, int weight) {
super(price, size);
this.weight = weight;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
void show(){
System.out.println("名称:"+name+"\n价格:"+getPrice()+"元\n大小:"+getSize()+"寸\n培根克数:"+getWeight()+"克");
}
}
class Pizza2 extends Pizza{
private String name="海鲜披萨";
private String ingredients;
public Pizza2(){}
public Pizza2(int price, int size, String ingredients) {
super(price, size);
this.ingredients = ingredients;
}
public String getIngredients() {
return ingredients;
}
public void setIngredients(String ingredients) {
this.ingredients = ingredients;
}
void show(){
System.out.println("名称:"+name+"\n价格:"+getPrice()+"元\n大小:"+getSize()+"寸\n配料信息:"+getIngredients());
}
}