欢迎关注我的公众号【软件大爆炸】
Java学习中的碎碎念
Java中易忽略的基础知识
Java面向对象基础
Java中的核心类
Java抽象类和接口
Java中的异常
Java中的泛型与集合
UML( Unified Modeling Language),统一建模语言
类与类之间存在六种关系
public class Test{
public static void main(String args[]){
Son s = new Son("zhengzhou","ZZU");
System.out.println(s.toString());
}
}
class Father{
String address;
public Father(String address){
this.address = address;
System.out.println("调用父类方法...");
}
}
class Son extends Father{
String school;
public Son(String address, String school) {
super(address);
this.school = school;
System.out.println("调用子类方法...");
}
@Override
public String toString() {
return "Son{" +
"address=" + address+'\'' +
"school='" + school + '\'' +
'}';
}
}
调用父类方法...
调用子类方法...
Son{address=zhengzhou'school='ZZU'}
在构造一个子类对象时,会首先调用父类的构造方法进行初始化,而后再调用子类的构造方法进行初始化
Java引用变量有两个类型:一个是编译时类型,一个是运行时类型。编译时类型由声明该变量所使用的类型决定;运行时类型则由实际赋给该变量的对象决定。如果编译时类型与运行时类型不一致,则称为“多态”。
一个父类具有多个子类,可以将子类对象直接赋值给一个父类引用变量,无需任何类型转换
例如:
Father s1 = new Son("zhengzhou", "ZZU");
Father s2 = new Son("Beijing", "THU");
//源码
public interface Move {
void move();
}
//源码
public class Animal implements Move {
@Override
public void move() {
System.out.println("Animal move");
}
}
public class Human implements Move{
@Override
public void move() {
System.out.println("Human move");
}
}
public class Car implements Move {
@Override
public void move() {
System.out.println("Car move");
}
}
public class MoveTest {
public static void main(String[] args) {
Move [] move = {new Animal(), new Human(), new Car()};
for (Move m : move) {
m.move();
}
}
}
//执行结果
Animal move
Human move
Car move
public class DependentDemo {
//Person 依赖 Car
public static void main(String[] args) {
Car car = new Car();
Person1 p = new Person1();
p.travel(car);
}
}
class Car{
void run(String spl){
System.out.println("开车去"+spl);
}
}
class Person1{
void travel(Car car){
car.run("ZZU");
}
}
依赖关系通常都是单向的。
并且人依赖车可以理解为:人的旅游依赖车,但并不关系车是如何得到的。只需要调用travel()方法时有车即可,旅游完毕后这辆车的去向也不必关心。
public class AssociationDemo {
public static void main(String[] args) {
Car car = new Car();
Person1 p = new Person1(car);
p.travel();
}
}
class Car{
void run(String spl){
System.out.println("开车去"+spl);
}
}
class Person1{
Car car = new Car();
Person1(Car car){
this.car = car;
}
void travel(){
car.run("ZZU");
}
}
人和车的关联关系可以理解为:人拥有辆车,旅游时可以用这辆车,做别的事情时也可以用。但是关联关系并不要求是独占的,以人车关联为例,即车也可以被别的人拥有。
public class AggregationDemo {
public static void main(String[] args) {
Employee[] emps = {
new Employee("xiaowang"),
new Employee("Laoli"),
new Employee("xiaozhao"),
};
Department dept = new Department(emps);
dept.show();
}
}
class Employee{
String name;
Employee(String name){
this.name = name;
}
}
class Department{
Employee[] emps;
Department(Employee[] emps){
this.emps = emps;
}
void show(){
for (Employee emp : emps){
System.out.println(emp.name);
}
}
}
xiaowang
Laoli
xiaozhao
上述代码中,部门类 Department中的 Employee数组代表此部门的员工。部门和员工的聚合关系可以理解为:部门由员工组成(从代码中可以明显看到),同一个员工也可能属于多个部门(可以构建其他部门类来包含员工),并且部门解散后,员工依然是存在的,并不会随之消亡。
public class CompostionDemo {
public static void main(String[] args) {
Engine engine = new Engine();
Chassis chassis = new Chassis();
Bodywork bodywork = new Bodywork();
Circuitry circuitry = new Circuitry();
Car0 car0 = new Car0(engine, chassis, bodywork, circuitry);
}
}
class Engine{
}
class Chassis{
}
class Bodywork{
}
class Circuitry{
}
class Car0{
Engine engine;
Chassis chassis;
Bodywork bodywork;
Circuitry circuitry;
public Car0(Engine engine, Chassis chassis, Bodywork bodywork, Circuitry circuitry) {
this.engine = engine;
this.chassis = chassis;
this.bodywork = bodywork;
this.circuitry = circuitry;
}
}
其中:
继承和实现体现的是类与类之间纵向关系,其他的四个则是横向关系。并且关联、聚合、组成更多的是语义上的区别,代码上无法区分。