2018-03-01
1. Create and instantiate a Java class
Packageday02;/** Object-oriented thinking of the ground rule one: * 1. Design the class and design the members of the class (member variables and • Member methods) * 2. Through the class, to create the object of the class (also become an instantiation of the Class) * 3. Called by object. Properties or object. method. */ Public classZoo { Public Static voidMain (string[] args) {//declaration of the base data type: Data type variable name = initialization value//int i=10; //instantiation of a class: The following A1 is a real objectAnimal a1=NewAnimal (); //such as arrays//int[] Arr=new int[10]; //calling properties by ObjectA1.name= "Floral"; A1.age=3; System.out.println ("Name:" +a1.name+ ' \ n ' + "Age:" +a1.age); //calling methods from objectsa1.eat (); A1.sleep (); }}classanimal{//1. PropertiesString name; intAge ; //2. Methods Public voideat () {System.out.println (Eating); } Public voidsleep () {System.out.println (Sleep); }}
2. Memory parsing for classes
Properties of the 3.Java class and local variables
The difference between a member variable and a local variable:
Same point: 1. Follow the format of the variable declaration: Data type variable name = Initialize value
2. There are scopes.
Different points: 1. The location of the declaration differs: Member variable: Declared in class, outside method
Local variables: Declared within a method, the formal parameter part of a method, within a code block
2. There are four modifiers for member variables: Public Private protected default
Local variable does not have the same modifier as the method in which it is located
3. Initialize value: There must be default initialization value
Member variables: If you do not explicitly assign a value at the time of declaration, the different data types will have different default initialization values.
Local variables: Be sure to assign values explicitly (no default initialization)
4. The two locations in memory are different: member variables stored in the heap space local variables stored in the stack space
Summary: The classification of variables: 1) varies by data type. Basic data Types (8) & Reference data types
2) According to the location of the declaration: Member variable local variable
4. Class method: Provide a function of the implementation
1) Example: public void Eat () {}
Public String GetName () {}
public void SetName (String n) {}
Format: Permission modifier return value type (void: no return value/specific return value) method name (formal parameter) {}
2) Note: About the return value type void means that this method does not require a return value
A method that has a return value: At the end of the method, the variable statement corresponding to the return+ return value type is returned, and the statement is not subsequently writable.
3) You can call other methods within the class, but you cannot define the method in the method.
Java Learning Diary------Object-oriented ②