Java core technology ---- objects Chapter Summary and heavy and difficult class

First, the relationship between classes

The relationship between the classes and the degree of coupling from high to low:
IS -a. Inheritance, realization
has-a. Combination, polymerization, associated
user-a. rely.
Requirements are: high cohesion and low coupling.

Inheritance ( "is-a")

Inheritance (Inheritance), i.e. "is-a" relationship, for a particular showing the general relationship. It represents a parent-child relationship class and class (or interface with the interface). In general, if a class A extended class B, class A from class B contains not only inherited method, but also has some additional features. In JAVA, represented by the keyword extends inheritance

  • Implement (Implementation), it represents a class method implements one or more interfaces. Well defined set of operations of the interface, the implementation class to perform specific operation of the interface. Use implements expressed in java.

Association ( "has-a")

Is the coupling between the class and class. It is known that the properties and methods of a class of another class. For two independent objects, when there is a fixed correspondence between specific examples of instances of an object with another object, for the association between the two objects. Association may be bidirectional, it can be one-way. In JAVA, association general use member variables to achieve. In JAVA, unidirectional association as follows: Class A Class B which uses, as a Class B type wherein A member variable. Bidirectional association as follows: Class A Class B which was used as a member variable; while Class B, Class A was also used as a member variable.
Unidirectional association:

Bidirectional associations:

class Car {   
    public static void run(){   
        System.out.println("汽车在奔跑");   
    }   
}   
     
class Driver {   
    //使用成员变量形式实现关联   
    Car mycar;   
    public void drive(){   
        mycar.run();   
    }   
} 

Dependent ( "has-a")

It is one of the most obvious, the most common relationship. Dependency represents a class depends on the definition of another class, a class method manipulation object of another class. In general, dependence is reflected in JAVA local variables, parameters, methods, or by calling the static method.

For example, a person (Driver) can drive (Car), Car Driver class depends on the definition of the class, because Driver class references Car. Is associated with a different, not Car Driver class attribute type, an instance of Car parameters are passed to the embodiment of the method to the Driver class.

class Car {   
    public static void run(){   
        System.out.println("汽车在奔跑");   
    }   
}   
     
class Driver {   
    //使用形参方式发生依赖关系   
    public void drive1(Car car){   
        car.run();   
    }   
    //使用局部变量发生依赖关系   
    public void drive2(){   
        Car car = new Car();   
        car.run();   
    }   
    //使用静态变量发生依赖关系   
    public void drive3(){   
        Car.run();   
    }   
}  

Second, access to Java classes

Java has four access rights, three of which have access modifiers are private, public and protected, there is a without any modifiers.
1, private: Java language to limit access to the narrowest modifiers, commonly known as "private." By its modified classes, properties, and methods can only be accessed by objects of this class, its subclasses can not visit, not to allow cross-packet access.
2, default: that is, without any access modifiers, commonly referred to as the "default access mode." In this mode, access is permitted only in the same package.
3, protect: an intermediate form between the access modifiers public and private, generally referred to as "protected-shaped." Modified by its class, properties, and methods can only be accessed by its own class and subclass method, even subclasses can be accessed in a different package.
4, public: Java language, access restrictions widest modifiers, commonly known as "public." By its modified classes, properties and methods can not only access across categories, and allows cross-package (package) access.
The following tabular form to show the similarities and differences between the four access rights, it will be more image. Table as follows:

Three, final keyword role

   1、final修饰类中的属性或者变量
          无论属性是基本类型还是引用类型,final所起的作用都是变量里面存放的“值”不能变。
          这个值,对于基本类型来说,变量里面放的就是实实在在的值,如1,“abc”等。
          而引用类型变量里面放的是个地址,所以用final修饰引用类型变量指的是它里面的地址不能变,并不是说这个地址所指向的对象或数组的内容不可以变,这个一定要注意。
          例如:类中有一个属性是final Person p=new Person("name"); 那么你不能对p进行重新赋值,但是可以改变p里面属性的值,p.setName('newName');
          final修饰属性,声明变量时可以不赋值,而且一旦赋值就不能被修改了。对final属性可以在三个地方赋值:声明时、初始化块中、构造方法中。总之一定要赋值。      
  2、final修饰类中的方法
         作用:可以被继承,但继承后不能被重写。
  3、final修饰类
         作用:类不可以被继承。

Four, static keyword role

    1、static可以修饰变量,方法
    2、被static修饰的变量或者方法是独立于该类的任何对象,也就是说,这些变量和方法不属于任何一个实例对象,而是被类的实例对象所共享。
    3、在类被加载的时候,就会去加载被static修饰的部分。
    4、被static修饰的变量或者方法是优先于对象存在的,也就是说当一个类加载完毕之后,即便没有创建对象,也可以去访问。

Fifth, whether Java object uses call by reference

    不是,在Java调用方法是采用“单向传值”,即调用方法时会将实参的值或地址拷贝一份给形参。形参的值(或地址)发生变化与实参无关,但如果是引用类型的可以改变所指对象中的值。
    也就是说调用方法是无法进行交换的无论是数值类型还是引用类型的变量,但可以改变引用类型所指对象的数据。

Sixth, the difference between overloading (Overload) and rewritable (Override) is. Overloaded method can be differentiated according to the type of return?

    答:方法的重载和重写都是实现多态的方式,区别在于前者实现的是编译时的多态性,而后者实现的是运行时的多态性。重载发生在一个类中,同名的方法如果有不同的参数列表(参数类型不同、参数个数不同或者二者都不同)则视为重载;重写发生在子类与父类之间,重写要求子类被重写方法与父类被重写方法有相同的参数列表,有兼容的返回类型,比父类被重写方法更好访问,不能比父类被重写方法声明更多的异常(里氏代换原则)。重载对返回类型没有特殊的要求,不能根据返回类型进行区分

Seven constructor constructor tune

    在Java中可以使用this调用此类的构造器,可以使用super调用父类的构造器
public A(int a){
    this();
    super();
}

VIII initializer block

    在Java中大家熟知的初始化成员变量的方法有:在声明中赋值和在构造器中设置值。其实还有第三种机制,称为初始化块。在一个类的声明中,可以包含多个代码块。只要构造类的对象,这些快就会被执行。例如:

class A{
    int a;       
    {
        a=10;
    }
}

Guess you like

Origin www.cnblogs.com/shaoyu/p/11517754.html