1. Class-to-object relationships
We learn programming language, is to simulate the real world things, to achieve information. For example: to the supermarket to buy things billing system, to the bank to transact business system.
How do we express a real-world thing?
Attribute: Is the descriptive information of the thing
Behavior: Is what the thing can do
For example, students have a name, age and other properties, students have to learn, play games and sleep and other behaviors.
The basic unit of the Java language is the class, so we should put things in a class to embody.
2. Definition of classes and objects
Class: is a set of related properties and behaviors.
Object: Is the concrete embodiment of this kind of thing.
3. Example
public class student{ string name; int age; string address; public void study () { system.out.println ("student Study"); } public void eat () { System.out.println ("The Student Eats"); } public void sleep () { system.out.println ("Student Sleeps"); }}public class studenttest{ public static void main (String[] args ) { student stu = new student (); stu.name = "haha"; Stu.age = 20; stu.address = "Jiangsu"; stu.study (); stu.eat (); stu.sleep (); }}
4. The difference between a member variable and a local variable
Different positions in the class
Outside the method in the member variable class
Inside a local variable method or on a method declaration
In-memory locations are different
Member Variable heap memory
Local variable stack memory
Different life cycle
The member variable exists as the object exists and disappears as the object disappears
Local variables exist with the invocation of the method and disappear as the call to the method finishes
Different initialization values
The member variable has a default initialization value
Local variables do not have default initialization values, they must be defined, assigned, before they can be used
"Caveats" The local variable name can be the same as the member variable name, when used in the method, using the nearest principle.
5. Anonymous objects
Anonymous object: An object without a name, is a simplified representation of an object.
Two use cases for anonymous objects: 1. Object Invocation method only one time 2. Pass as actual parameter
This article is from the "11831428" blog, please be sure to keep this source http://11841428.blog.51cto.com/11831428/1856056
Java-like and objects