1, object-oriented programming of the three basic special: encapsulation, inheritance, Polymorphism.
Encapsulation: put data and methods in a class.
2. Define the Class:
class the name of the class { the contents of the class}
Definition method: (the method definition cannot be nested and another method cannot be defined in one method.) )
Modifier returns a value type method name ([parameter 1, parameter 2, parameter N]) { method body;}
To build the Object:
public class = new person ();
Example:
public classtest{//method of adding two integers public intAddintAintB) {returnA +b; } //method of subtracting two integers public intSubtractintAintB) {returnAb; } //two integers multiplied by a method public intMultiplyintAintB) {returnAb; } //a method for dividing two integers public intDivideintAintB) {returnAb; } //use the Void keyword to indicate that a method does not return a value public voidoutput () {System.out.println ("Hello world"); } public Static voidmain (string[] Args) {test test=NewTest (); intx = 8; inty = 3; intA =Test.add (x, y); intb =test.subtract (x, y); intc =test.multiply (x, y); intD =test.divide (x, y); System.out.println (a); System.out.println (b); System.out.println (c); System.out.println (d); Test.output (); }}
3, Reference Type. An object can be pointed to by multiple references, and the same reference can point to only one object at a Time. When multiple references point to an object, any reference that changes the member variable of the object is reflected on the other reference.
If a class contains properties and methods, each object generated by the class maintains one of its own objects, but it also has the same method.
public classpeople{intAge=20; public voidchange (people People) {people=Newpeople (); People.age=30; System.out.println (age);// -System.out.println (people.age);// -} public Static voidmain (string[] Args) {people people=Newpeople (); intAge =people.age;//is The native data type that is passed directly to the Value. System.out.println (age);// -People.change (people); intAge2=people.age; System.out.println (age2);// -}}
4, construction Method: used to complete the initialization of the Object.
Features: First: The name must be the same as the class Name. Two: there is no return value, and no void can Occur. Third: If there is no construction method in the class, Java is added by default as an empty Constructor. If a construction method is declared, the Java compiler will no longer add a constructor method. Four: implicit invocation via new
New builds the Object's three steps: to open up memory space for the object, call the constructor method of the class, return the address of the generated Object.
public classparamtest{ public Static voidmain (string[] Args) { person person=Newperson (); Person.change (person); intAge = person.age;// -System.out.println (age); inti = 10; Person.change2 (i); System.out.println (i);//TenSystem.out.println (person.age);// +System.out.println (age);// - }}classperson{intAge = 20; public voidChange (person person ) { person=Newperson (); Person.age= 30; } public voidChange2 (intage ) { this. Age = 40; }}
8, Construction method overloading: only need to see the Parameters. If another constructor method is called in one of the constructor methods, it can be called using the This () method. The parameters in this () brackets represent the parameters of the target construction method. This () must be the first sentence of the constructor method.
public class constructoroverload{ public constructoroverload () { this (3); System.out.println ( "test" public constructoroverload (int Span style= "color: #000000" > i) {System.out.println ( ++i); public static void main (string[] Args) {constructoroverload col = new constructoroverload (); }}
//4 Test
9, Inheritance. Java is a single inheritance. When generating a subclass object, Java defaults to calling the parent Class's constructor without parameters, generating the parent class object, and then calling the subclass construction method to generate the subclass Object.
public classinheritencetest3{ public Static voidmain (string[] Args) {son son=NewSon (); }}classgrandpa{ publicGrandpa () {System.out.println ("grandpa"); }}classFatherextendsgrandpa{ publicFather () {System.out.println ("father"); }}classSonextendsfather{ publicSon () {System.out.println ("son"); }}//Grandpa father son
10. Super keyword, which represents a reference to the parent class Object. You can make the subclass call the parent class with the constructor of the parameter, which must be placed on the first line (the purpose is to ensure that Mr. is the parent class object).
When the parent class does not have an parameterless constructor, the constructor of the calling parent class must be displayed using the Super Keyword.
public classinheritencetest2{ public Static voidmain (string[] Args) {dog dog=NewDog (); Dog.run (); } }classanimal{ public voidrun () {System.out.println ("animal is running"); } public voidRunintI) {System.out.println ("animal is sleepping"); }}classDogextendsanimal{ public voidrun () {System.out.println ("dog is running"); Super. Run (1);//call the Run method of the parent class }}//Dog is runningAnimal is sleepping
self-learning Java Fundamentals (iii)