Java语言中类与类之间的关系
Java语言中类与类之间的关系有三种,分别是关联关系、依赖关系和继承关系。
- 关联关系:一个对象有另一个对象的引用,关联可以是单向的也可以是双向的。
“`Java
public class SmartCar {
private Wheel[] wheels=new Wheel[4];
private Engine engine=new Engine();
public void drive(Instruction instruction){
switch(instruction.getContent()){
case"speed-up":engine.setSpeed(instruction.getParam());
break;
case"slow-down":engine.setSpeed(instruction.getParam());
break;
case"nomal":engine.setSpeed(instruction.getParam());
}
System.out.println("车车正在以"+engine.getSpeed()+"在行驶");
}
public Wheel[] getWheels() {
return wheels;
}
public void setWheels(Wheel[] wheels) {
this.wheels = wheels;
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
此时类SmartCar用到了wheels和engine 类作引用
依赖关系:类A使用到了类B,类B的变化会影响到类A,比如我要写一篇博客,需要一台电脑,此时我与电脑便是依赖关系。
“` Java
public class Instruction {private String content;
/**
- 参数
*/
private int param;
public String getContent() {
return content;
}public void setContent(String content) {
this.content = content;
}public int getParam() {
return param;
}public void setParam(int param) {
this.param = param;
}- 参数
}
public class App {
public static void main(String[] args) {
SmartCar car=new SmartCar();
Instruction instruction=new Instruction();
instruction.setContent(“speed-up”);
instruction.setParam(120);
car.drive(instruction);
Banana c=new Banana();
}
}
- 在上面关联关系代码中的SmartCar类用到了下面Instruction类中的方法,这种关系变是依赖关系,在App中也可以知道,如果Instruction类加以改变,便会影响到类SmartCar。
继承关系:一个类B继承另一个类A的功能吗,并可以增加它自己的新功能,A为父类,B为子类,通过extends确定父子关系。比如一个父类是人,子类为男人,可以说男人是人,但是反过来说人是男人,就不对了。
“` Java
public class Worker {private String name;
private String address;
private double salary;public void show(){
System.out.println(name+”住在”+address+”每个月工资:”+salary+”元”);
}
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 getAslary() {
return salary;
}
public void setAslary(double aslary) {
this.salary = aslary;
}public Worker(String name,String address,double salary){
this.name=name;
this.address=address;
this.salary=salary;
}
}
public class Manager extends Worker{private String department;
public String getDepartment() {
return department;
}public void setDepartment(String department) {
this.department = department;
}public void show(){
System.out.println(“=====================”);
super.show();
System.out.println(“所管部门为:”+department);
}public Manager(String department){
super(“”,”“,0);
this.department=department;
}
}
public class Director extends Worker{private double allowance;
public double getAllowance() {
return allowance;
}public void setAllowance(double allowance) {
this.allowance = allowance;
}public void show(){
System.out.println(“=====================”);
super.show();
System.out.println(“交通补助有:”+allowance+”元”);
}public Director(double allowance){
super(“”,”“,0);
this.allowance=allowance;
}
}
public class App3 {
public static void main(String[] args) {Worker a =new Worker(“王二”,”石桥铺”,3000);
a.setName(“王二”);
a.setAddress(“石桥铺”);
a.setAslary(3000);
a.show();Director b=new Director(200);
b.setName(“王五”);
b.setAddress(“沙坪坝”);
b.setAslary(5000);
b.setAllowance(200);
b.show();Manager c=new Manager(“人事部”);
c.setName(“王五”);
c.setAddress(“大坪”);
c.setAslary(7000);
c.setDepartment(“人事部”);
c.show();
}
}- 在这里Work类为父类,Director类和Manager类继承了父类的功能和属性,并在App3中验证了这一点。