Java: class4 Classes and Objects

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43336822/article/details/95598730

1. class: the definition of a student class, student class mainly consists of two parts

What is the property :() class People {
name String name;
age, int age; // attributes: Field / member variables / instance variables
Student ID

Behavior :( I can do)
eat
sleep void eat () {} // behavior: Method
Learning}

Instantiate: People p = new People (); // opens up memory

Constructor: People () {} // void and the like can not be added, i.e. not have a return value

.New 2:
.. 1) to open heap memory
2) call the constructor to initialize instance variables.

3. Local variables can not be used uninitialized; uninitialized member may be used.

4. The source file can not have a plurality of public class.

In 5.CMD you can see the process by jps.

6. The method defined variables do not account for the heap memory.
Student {class
// class header: 8 bytes
int age = 20; // occupies 8 bytes
}

7. constructor: If no constructor, the system automatically provides a no-argument constructor; while he wrote a constructor, the system is no longer available.
1) name of the constructor must be the same as the class name, including capitalization;
.. 2) constructor has no return value, nor can void if modifications are not careful to add a front constructor return type, then this will make this constructors become a common method, the runtime error constructor can not be found.
3. A class defines a plurality of construction method, if the constructor is not defined in the definition of the class, the compiler will automatically insert a default constructor with no arguments, the constructor does not execute any code.
4. The method of construction may be overloaded to the number of parameters, type, order

8. The access modifier:
public: public, anyone can use.
private: private, it can only be used in the current class.
package: the same package can access the path, it is the default. Written in the beginning of the current in the unusual class packages: import.package.src2 (package.src2 to a reference path and the like)

The method can be used in this keyword: this represents the object passed in. // when there is a naming conflict, we need to add this keyword to indicate the current object

10. The string passed to the function, the function added remember if (str == null) return;

11. A copy of the object : a clone (), needs to be added to the clone in the class Object () method rewritten:
Further to add, after class cat implements Cloneable.
1) a shallow copy of memory sharing, modify one another attribute change. If only in a class of type int and String attributes, you can use this.
the implements the Cloneable the Address {class
int ID;
}
}
protected Object clone () throws CloneNotSupportException {
return to super.clone ();
}
2) deep copy, the elimination of memory sharing. If included in a class of their own definition of some of the properties, use this, otherwise there is a shared memory.

class Address implements Cloneable{
        int id;
        @Override              //需要对这个特有的属性也进行clone的重载
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }
    class Cat implements Cloneable{
        int age;
        Address address = new Address();
        @Override
        protected Object clone() throws CloneNotSupportedException {
        Object o =  super.clone();//
        Cat c = (Cat)o;
        c.address = (Address) this.address.clone();
        return c;  //返回的是对象
        }
    }

Guess you like

Origin blog.csdn.net/qq_43336822/article/details/95598730