Jvdy3 (上)

Java learning_Day3(上)

I use the video to learn the Mashi Bing, is here presented
<link: https://pan.baidu.com/s/1qKNGJNh0GgvlJnitTJGqgA >
extraction code: fobs

content

  • Java object-oriented programming
  • The concept of objects and classes
  • Constructor (the constructor)

Java object-oriented programming

  • The basic idea is object-oriented, real-world things from an objective viewpoint to construct software systems, and the use of natural human way of thinking as far as possible in the construction of the system.
  • Object greater emphasis on the use of human thinking and principles in everyday thinking logic often used, such as abstraction, classification, inheritance, aggregation, polymorphism, etc. oriented.

The concept of objects and classes

  • Object by "attribute (attribute)" and "method (Method)" respectively correspond to things that have static and dynamic properties.
  • Class is used to describe the same type of object of an abstract class that defines the object class should have the static and dynamic properties.
  • Class can be seen as a template for a class of objects, objects can be viewed as a specific example of the class of change.

connection relation

  • Parameters often a method is an object of another class
  • No close relationship

Inheritance

  • XX is satisfied XX (XX is a XX)

Aggregation relationship

  • And part of the overall
  • Polymerization can be subdivided into aggregates and combinations
    • Aggregation relationship may belong to a plurality of integrally part, act as different objects.
    • Partially assembled relationship is part of a whole, it acts as a single object.

Realization relationship

  • For certain subclasses of the parent class implementation

Polymorphism

Creating a simple object with properties and methods

public class Dog {
    //创建狗这个类
    
    char furColor;
    char voice;
    int height;
    int weight;
    
    //狗拥有抓老鼠这个方法
    public void catchMouse(Mouse mouse) {
        //catch mouse
        mouse.scream();  //老鼠有发出尖叫这个动作(方法)
    }

    public static void main(String[] args) {
        Dog dog = new Dog();  //创建狗这个对象
        Mouse mouse = new Mouse();  //创建老鼠这个对象
        dog.catchMouse(mouse);  //狗拿耗子
    }
}

Class definition

  • Class definition is mainly composed of two parts
    1. Member variables
    2. Methods
      Format 1 declare member variables are:[<modifiers>] type <arrt_name>[=defaultValue];

Member variables

  • Member variables can use any data type in the Java language (including basic types and reference types)
  • In its definition of member variables can be initialized, if not its initialization, Java initializes it with the default values ​​in the following table.
  • Member variable type The value
    byte 0
    short 0
    int 0
    long 0L
    char '\u0000'
    float 0.0F
    double 0.0D
    boolean false
    All reference types NULL

Creating and using objects

  • 必须使用new关键字创建对象。
  • 使用对象(引用).成员变量来引用对象的成员变量。
  • 使用对象(引用).方法(参数列表)来调用对象的方法。
  • 同一类的每个对象有不同的成员变量存储空间。
  • 同一类的每个对象共享该类的方法。

构造方法(构造函数)

  • 使用new+构造方法创建一个新的对象。
  • 构造函数是定义在Java类中的一个用来初始化对象的函数。
  • 构造函数与类同名且没有返回值。
public class Person {
    int id;
    int age;

    Person(int n, int i) {  //Person类的构造函数
        id = n;
        age = i;
    }
}

创建对象时,使用构造函数初始化对象的成员变量。

class Test {

    public static void main(String[] args) {
        Person tom = new Person(1, 25);
        Person john = new Person(2, 27);
    }   
}

当没有指定构造函数时,编译器为类自动添加形如 类名() { } 的无参数构造函数
注意:一旦手动添加了构造函数,编译器将不再默认添加一个无参数构造函数,此时初始化对应的无参数对象编译器将会报错

构造函数对对象初始化后,如何改变对象存储的值? 详见:https://www.cnblogs.com/HuoHua2020/p/12331890.html

Guess you like

Origin www.cnblogs.com/HuoHua2020/p/12331875.html