Java中类与类的关系

Java中类与类的关系:依赖关系、关联关系、继承关系、实现关系(本质也是继承关系)


  • 依赖关系((use-a):类A使用到类B中;关系特点:关系脆弱,表现出偶然性与临时性,且类B的变化会直接影响到类A。
    • A类依赖B类:即B类的对象作为A类的方法参数存在。当且仅当A类调用到含有B类对象作为A类中方法的参数时,B类对象才被临时创建。方法执行结束,临时创建的B类对象立即被回收。(即关系破裂,有点过河拆桥的意思)也就是说,只有在A类用得到B类对象作为方法参数时,A依赖B关系才存在。eg:日常生活中——刷牙。人()、牙刷(牙刷类的具体实例)、刷牙(行为方法)。牙刷(一个实例)作为刷牙方法的工具(参数)被人(类)调用,刷牙结束,人就不再依赖牙刷刷牙。
    • 代码体现
public class Person {
    /**
     * 属性
     */
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void cleanTeeth(Toothbrush toothbrush) {
        System.out.println(name + "正在用" + toothbrush.getProducer() + "牙刷刷牙");
    }
}

public class Toothbrush {
    private String producer;
    private double price;

    public String getProducer() {
        return producer;
    }

    public void setProducer(String producer) {
        this.producer = producer;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

}
public class Demo {
    public static void main(String[] args) {
        Person p = new Person();
        Toothbrush toothbrush = new Toothbrush();
        toothbrush.setProducer("佳洁士");
        p.setName("小王");
        p.cleanTeeth(toothbrush);
    }
}

  • 关联关系(has-a):A类作为B类的属性类型存在。
    • 单向关联:如果A类作为B类的属性类型存在,那么我们说A类关联B类。此时可将A类作为引用数据类型看待。(商榷……)
    • 补充:属性是一个类数据的体现。eg:颜色是水果类的一个属性。
    • 代码体现:
public class Fruit {
    private String name;
    private double weight;
    private Color color;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }
}

public class Color {
    private String description;

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

public class Test {
    public static void main(String[] args) {
        Fruit fruit = new Fruit();
        Color color = new Color();
        fruit.setName("Apple");
        color.setDescription("red");
        fruit.setColor(color);
        System.out.println(fruit.getName() + "\tis the\t" + fruit.getColor().getDescription());
    }
}
  • 双向关联:A类关联B类,B类关联A类;即A类作为B类的属性类型存在的同时,B类也作为A类的属性类型存在。如果互相关联的类中存在整体和部分的关系,那么此时的关联关系分为: 聚合和组合
  • 聚合与组合的主要区别在于生命周期的不同。
    • 聚合:特立独行。(互相关联的类的实例)
    • 组合:同生共死。(互相关联的类的实例)
  • 代码体现: 人类与手机类的互相关联。
public  class Person {
    /*------属性-----*/
    private String name;//姓名
    private int age;
    private Phone phone;
    public Phone getPhone() {
        return phone;
    }
    public void setPhone(Phone phone) {
        this.phone = phone;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

}

public class Phone {

    private String brand ;//品牌
    private String type;//型号
    private Person per;
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public Person getPer() {
        return per;
    }
    public void setPer(Person per) {
        this.per = per;
    }   
}

  • 继承(extends)关系(is—a):eg. A类继承B类:A extends B, 则A类为“子类”,B类为“父类”。一个子类只能有一个父类(单继承性,保证代码的可靠性),而一个父类可以有多个子类,且:A类与B类之间需要特定的关系。当A类继承B类后,从B类那继承到B类的所有属性和方法;(private属性、方法不能被继承)
  • Object类是所有类的父类。
    • 几个小结论:基本类型的数组不是Object数组的子类。
    • 引用类型的数组是object数组的子类。
    • 数组是Object的子类
      *关键字:extends、super、protected 、this 。
    • 同包子类可以访问父类的public/protected/(default) 方法。
    • 不同包子类可以访问父类的public/protected。
    • 继承的default方法按照父类计算是否同包。
    • 访问权限修饰符的规则是一样的。
  • 继承中的构造方法
    • 先执行父类的构造方法,再执行子类的构造方法。
    • 默认情况下,调用父类的无参构造方法。
    • super关键字可以调用父类的构造方法,放在构造方法的第一行。
    • this关键字可以调用本类的构造方法,放在构造方法的第一行。
  • this/super
    • this 当前对象本身,super 父类的对象。this 可以调用属性、方法、构造方法、引用本身;super 调用父类可访问的构造方法、方法、属性。
  • 方法重写(override):方法名、参数、返回值类型相同,访问权限修饰符不能低于父类。
    • 新建的实例访问的方法是子类的覆盖后的方法。
      *代码体现
public class Personnel {
    private String name;
    private String address;
    private double basicWage;

    public Personnel(String name, String address, double basicWage) {
        this.name = name;
        this.address = address;
        this.basicWage = basicWage;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public double getBasicWage() {
        return basicWage;
    }

    public void setBasicWage(double basicWage) {
        this.basicWage = basicWage;

    }

    public void show() {
        System.out.println("员工的姓名:" + name + "\n家庭住址:" + address + "\n基本工资:" + basicWage);
    }
}

public class Manager extends Personnel {

    private String department;

    public Manager(String name, String address, double basicWage, String department) {
        super(name, address, basicWage);
        this.setDepartment(department);
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public void show() {
        System.out.println("经理详细信息:\n-----------\n姓名:" + getName());
        System.out.println("家庭住址:" + getAddress());
        System.out.println("基本工资:" + getBasicWage());
        System.out.println("所属部门:" + getDepartment());
    }

}

public class Director extends Personnel {

    private int travelAllowance;

    public Director(String name, String address, double basicWage, int travelAllowance) {
        super(name, address, basicWage);
        this.setTravelAllowance(travelAllowance);
    }

    public int getTravelAllowance() {
        return travelAllowance;
    }

    public void setTravelAllowance(int travelAllowance) {
        this.travelAllowance = travelAllowance;
    }

    public void show() {
        System.out.println("董事信息:\n------------");
        System.out.println("员工姓名:" + getName());
        System.out.println("家庭住址:" + getAddress());
        System.out.println("基本工资:" + getBasicWage());
        System.out.println("差旅津贴:" + getTravelAllowance() + "\n");
    }

}

public class AppPersonnel {
    public static void main(String[] args) {
        Personnel employee0 = new Director("老王", "北京", 15000, 1256);
        Personnel employee1 = new Manager("小明", "重庆", 4000, "酱油");
        employee0.show();
        employee1.show();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值