re0:从零开始的Java学习生活09(连载)

接口、设计模式

一、面向对象练习

做题四大原则:

一、继承链:自己没有找父

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());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值