3/16/2010 study notes -day19

java-day19

table of Contents

morning

 Create and initialize objects

Memory map

 Method Invocation memory map

in the afternoon

Constructor

The role of the constructor

Overloaded constructor

Transcoding

          inherit

Subclass constructor calls the parent class has two kinds of situations:


morning

OverLoadTest o = new OverLoadTest();
o.test(null);

Note that when there are a number of overloaded methods, parameters, these methods are reference types, and this time if you call the method directly and pass - a null value, then the compiler is likely compile error, because if these methods are overloaded parameter types, there are sub-parent relationship, then the sub-priority call type parameter method, if a reference type parameter is not child-parent relationship, the compiler will error.

 Create and initialize objects

Create an object using the new keyword when, in addition to the allocated memory space, but also to object attributes make the default initialization assignment, and call the class constructor.

E.g:

new Student(); 
//这个代码在main方法中执行了

1. The memory space allocated to the object, and the object are initialized, the digital type is 0, the type of reference is null, Boolean is false.

2. If we do a display assigned to the variables in the object, it will show with this assignment, the default value prior to overwrite.

E.g:

private String name = "Tom";

3. Call class constructor is executed in the constructor code

Note that if this time there is a variable to receive the object (= number assignment), then the time will return the object memory address values ​​to this reference type variable.

E.g:

//stu最终保存了对象的内存地址值。
Student stu  = new Student();

Memory map

 Method Invocation memory map

 

in the afternoon

Constructor

Class constructor, also known as construction methods, constructors, is created when the object to be called, and the structure has the following two characteristics:

1, must be consistent with the name of the class

2, there must be no return type, can not write void

 

The role of the constructor

1, using the new keyword to create an object when, with the back of the class must be present in the constructor

2, the constructor code, create regret is called an object, which can complete the initialization of the object.

public class Student{
private String name;
public Student(){}
public Student(){
    this.name = "default-value";
}

Overloaded constructor

In addition to the default no-argument constructor, there are many times we will use argument constructor, when creating the object, we can put the argument to the constructor, and then complete the initialization of the object.

Transcoding

String str = "你好";
byte[] arr = str.getBytes();
System.out.println(Arrays.toString(arr));
String newStr = new string(arr);
System.out.println(newStr);

inherit

A relationship between 1 and class is the class inheritance

There are many relationships between classes and java classes, only a succession, there is dependent, in combination, polymerization.

 

2, two of the class hierarchy, a subclass (derived class), is a superclass (class-yl)

Inherit the parent class extends keyword

public class Student extends Person{
    
}

3, the relationship between parent and child class, is the "is a" relationship from the sense

public class dog extends animal{}
//dog is a animal

4, the java inheritance between classes and a single base

Between class and class, can have between interfaces and interface inheritance relationships between classes and it is the interface and the interface between single inheritance, multiple inheritance is single inheritance means that a class can have a [direct ] of the parent type, but a class might be a lot of super class, super class refers to a class of the parent type

5, the parent class attributes and methods may be inherited by subclasses

Subclass constructor calls the parent class has two kinds of situations:

1, an implicit call

If there is a default no-argument constructor in the parent class, then subclass it will default default implicit automatically call the parent class constructor without parameters

2, show the call

If the parent class is no default no-argument constructor, then we need to subclass manual explicitly call the parent class constructor has parameters

 

6, each class If you do not specify a parent type, the compiler directly inherit the Object class.

Java so that each of the class directly or indirectly inherits Object class, and each object has and Object is a relationship of

public class Student extends Object{
    
    
}
package com.zzb.day19;
​
public class Student extends Person{
    
    public static void main(String[] args){
        Student stu = new Student();
        stu.name="Tom";
        System.out.println(stu.name);
        stu.run();
    }
}
package com.zzb.day19;
​
public class Person{
    
        public String name;
        public void run(){
            System.out.println("people run....");
        }
​
}

Published 82 original articles · won praise 52 · views 40000 +

Guess you like

Origin blog.csdn.net/iostream992/article/details/104905673