提示:文章详细的讲解了类图的四种关系,以及每种关系如何转化为对应的代码。
public class TV{
private int color;
public void change(Channel c){}
public void turn_on(){}
public void turn_off(){}
}
public class Channel{}
public Tank{
public void ram(){}
public void radion(){}
}
public Special extends tank{}
public class ClassA{
private ClassB classB;
}
public class ClassB{}
public class Company{
private Person[] persons;
}
public class Person{
private Company company;
}
public class WorkerPerson{
private WorkerPerson workerPerson;
}
public class Car{
private Engien engien;
private Wheel wheel;
public Car(Engien engien, Wheel wheel){
this.engien = engien;
this.wheel = wheel;
}
}
public OnePerson{
private Head head;
public OnePerson(){
head = new Head();
}
}
public class Head{}
...
通过 implements 关键字实现。
public interface IMyInterface{
public String g;
public int i;
public int charge();
}
public class MyClass implements IMyInterface{}
public class User{
private String name;
private String email;
private String password;
private List<Cart> carts;
public User(List<Cart> carts){
this.carts = carts;
}
}
public class Product{
private int id;
public String name;
public Float price;
}
public class Cart{
public List<Product> products;
public int add_product(int product){}
public Float get_total_price(){}
}