Class object
Class object (according to English is the thing, but experts say that is the object so we learn objects)
Class is the type
Because objects object = = "Everything is something ==> everything is the object" = = Experts say everything is the object
is structured data
Class is the classification of objects
Class is the object's data structure definition = = "Object has what is defined by the class
Create object, need to have type, create object class with type is structure template of object
After the object is created, the member variable can be initialized as the default.
Numeric type 0
int 0
BYTE 0
Short 0
Long 0L
Float 0.0f
Double 0
Boolean type False
Char type \u0000
Reference type NULL
Reference type
Variables declared with the class name (interface \ Array) other than 8 basic types are reference type variables
The value of a reference type variable stores the address information of an object in memory, and the function of the application is to access the object.
Method:
Defining in a class
The behavior that represents an object is a function
The implementation of the method is realized by using the data of the algorithm Operation object
Invoking methods with objects
this keyword
This represents the "this" object that invokes the method during method execution. is dynamically bound to the current object, and the method identifies the call to the object through this in the method area.
Represents the current object itself as an implied parameter of a method, accepting an object reference
Essence
Inside the stack is the data
The method area is an algorithm
package day15;public class demo01 {public Static void main (String[] args) {dog wangcai = new dog (); wangcai.age=3 ; Wangcai.name= "Wangcai"; wangcai.sex= ' m '; wangcai.color=0x505050; System.out.println (Wangcai.age); System.out.println (Wangcai.name); System.out.println (Wangcai.sex); System.out.println (Wangcai.color); Wangcai.eat ();}} class dog{string name; //instance variables : Each object instance has a nameint age;char sex;int Color;public void eat () {System.out.println ("eat");}}
Package Day15;public class Demo01 {public static void main (string[] args) {cell C1 = new cell (); c1.row = 1;c1.col = 5;c1.c Olor = 0x505050;cell C2 = new cell (); c2.row = 1;c2.col = 6;c2.color = 0x505050; System.out.println (C1.row); C1.drop (); C2.drop (); System.out.println (C1.row); System.out.println (C2.row);}} Class Cell {int row;int col;int color;public void Drop () {//Move down row++;} public void Leftshift () {//left shift col--;} public void RightShift () {//Right shift col++;}}
A class with the same name cannot be declared in the same package
Constructors (makes creating objects easy and concise)
Construction methods: Methods for creating and initializing objects (initialization properties)
Construction methods are declared in the class
Method Name of construction method is consistent with class name
Construction method cannot declare return value
Call the constructor with the new operation to create the object
Java does not recognize constructors based on constructor parameters
Package day15;public class demo01 {public static void main (String[] args) {dog xiaowangcai = new dog ("Xiaowangcai", 5, ' m ', 0x404040);// Contrast Wangcai's statement Xiaowangcai's statement is more convenient//dog xiaowangcai2 = new dog ("Xiaowangcai2", 4, ' W ', 0x606060);// It's easy to create objects. Xiaowangcai.eat ();//xiaowangcai2.eat ();}} class dog {string name; // instance variables : Each object instance has a nameint age;char sex; Int color;public dog (String name, int age, char sex, int color) {// Constructor (construction method): For more convenient System.out.println (this.name);this.name = name;// The parameters in the method are local variable System.out.println (this.age), This.age = age;// this.age is the instance variable System.out.println ( This.sex);this.sex = sex;// use this. to differentiate the method parameters and instance variables System.out.println (This.color) in order to distinguish ; This.color = color;} Public void eat () {// method System.out.println ("Eat");}}
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/74/07/wKiom1YOOh7hnk4GAAehJ80h9jc819.jpg "title=" 1.png " alt= "Wkiom1yooh7hnk4gaaehj80h9jc819.jpg"/>
Java Construction methods and methods
Construction method (constructor, constructor) comparison method
Constructor method: The method name must be the same as the class name, cannot define a return value try the new operation call construct FANGFA
Method: Method name is generally not the same as the type, must define the return value does not return the value of the definition of using void Trial Reference call method
Construction method: Used to create object initialization properties: Create a small Wang Choi
Method: Is the behavior function of the object: the dog eats.
Types with nouns
Methods with verbs
noun + verb
Overload:
Same name, different function.
Operator: + numeric Add Connection string
Method overloading: Same as method name, different parameters
Constructor Reload: Same as constructor method name, different parameters
Package day15;public class demo01 {public static void main (String[] args) {dog xiaowangcai = new dog ("Xiaowangcai", 5);// Compared to WANGCAI's statement Xiaowangcai's statement is more convenient Dog xiaowangcai2 = new dog ("Xiaowangcai2", 4, ' W ', 0x606060);// It's easy to create objects. Xiaowangcai.eat ();// calls the overloaded method Xiaowangcai2.eat ("Bone");// calls the overloaded method}}class dog {string name ; // instance variables : Each object instance has a nameint age; // property char sex;int color;public dog (String name, int age) {// Constructor (construction method): For more convenient this.name = name;// The parameter in the method is the local variable this.age = age;// this.age is the instance variable System.out.println (this.name); System.out.println (this.age);} constructor overloads parameters are different no sex and Colorpublic dog (string name, int age, Char sex, int color) {// Constructors (construction method): For more convenient this.name = name;// methods in the parameterThe number is a local variable this.age = age;// this.age is an instance variable this.sex = sex;// in order to distinguish use this. Distinguishing method parameters from instance variables this.color = color; System.out.println (this.name); System.out.println (This.age); System.out.println (This.sex); System.out.println (This.color);} Public void eat () {// method System.out.println ("Eat");} Overloaded method, the method name is the same, the parameters are different// calls have parameters to execute the following public void eat (STRING STR) {// Method System.out.println ("Eat" + str);}}
Null
Null pointer exception: The value of the reference variable is null, no reference to any object
Null pointer exception occurs when a method property is called on a null reference
Assign a value to a reference variable before accessing the method property so that the variable refers to the object = = "resolves
There are no objects with variables
Accessing properties
Calling methods
Finally: object is something!!!
This article is from the "Romantic Laugh" blog, make sure to keep this source http://lmdtx.blog.51cto.com/6942028/1699923
JAVA15: Object-oriented