Method construction and method overloading of Ultraman and the Battle of the Big Boss

Source: Internet
Author: User

Summary of Knowledge points :

1. The methods in the class are divided into two categories: 1. Common method; 2. Construction method.

2. Format of the construction method:
public class name (data type parameter name,...) {
}

3. Use of construction methods:
1. Instantiate the object.
2. Initialize the property value.

4. The difference between the construction method and the common method:
1. Return value: The construction method must not define a return value type; the name of the construction method has to be exactly the same as the class name, including case.
2. Use
3. Name

5. Note: Java provides a parameterless constructor for a class (the default constructor method), if you define a constructor method in the class,
The default construction method is overwritten and cannot be used by default, only using your custom constructor.

6. Conditions for method overloading:
1. The method name must be fully consistent;
2. The method takes the number of parameters, the parameter order, the parameter type at least one of the different.


For example:

protected int study (int  s) {        + = s;        SYSTEM.OUT.PRINTLN (name+ "credits are" +score);         return 0;    }          Public void Study () {        score+ +;        SYSTEM.OUT.PRINTLN (name+ "is learning, credits are" +score);    }


7. What kind of situations in real life do you want to use method overloading?
Overloading of common methods:
Learn to drive, learn to play games
Overloading of construction methods:
Jia Baoyu was born with Tong Lingbao Jade, the other role is not need to bring Tong Lingbao Jade.

8. Calling an overloaded method is determined by the arguments given when the method is called.

 Public Static void Main (string[] args) {  new niming ("Zhang San", 3);  Stu.study ();  Niming  new niming ("John Doe", 3);      = "John Doe";  Stu2.study (ten);   

9. When you have the name of the parameter and the property name exactly, add "this." to the attribute name, so that the attribute is represented, otherwise the argument is represented. (because of the nearest principle, the nearest is the parameter, not the attribute)
This represents the object that is currently calling the method.
Suppose that the a object name is called in the method, then this in the method represents the A object.

 public niming (String name,int.  score) {        this. Name = name;          this. score =score;    }

Instance:

 Packages522; Public classRole { PublicString name;  Public intBlood;  Public intattack; //defining a construction method  PublicRole (String name,intBloodintattack) {      This. name=name;  This. blood=Blood;  This. attack=attack;}  Public voidPK (role role) {Role.blood-=attack; SYSTEM.OUT.PRINTLN (Name+ "attacking" + Role.name + "," +Role.name+ "Remaining amount of blood is:" +Role.blood); }     Public Static voidMain (string[] args) {Role A1=NewRole ("Ultraman a", 100,5); Role A2=NewRole ("Ultraman B", 100,5); Role M1=NewRole ("Little Monster a", 100,3); Role m2=NewRole ("Little Monster B", 100,3);  while(A1.blood > 0 && m1.blood > 0) {A1.            PK (M1); if(M1.blood <= 0) {System.out.println (A1.name+ "Victory!" ");  Break; } M1.            PK (A1); if(A1.blood <= 0) {System.out.println (M1.name+ "Victory!" ");  Break; }        }        if(A1.blood > 0) {             while(A1.blood > 0 && a2.blood > 0) {A1.                PK (A2); if(A2.blood <= 0) {System.out.println (A1.name+ "Victory!" ");  Break; } A2.                PK (A1); if(A1.blood <= 0) {System.out.println (A2.name+ "Victory!" ");  Break; }            }        }        Else {             while(M2.blood > 0 && m1.blood > 0) {m2.                PK (M1); if(M1.blood <= 0) {System.out.println (M2.name+ "Victory!" ");  Break; } M1.                PK (m2); if(M2.blood <= 0) {System.out.println (M1.name+ "Victory!" ");  Break; }            }        }    }}

The above method uses the construction method, does not use the method overload, if wants to use the method overload, the idea is: writes two method body, one is Ultraman plays the small monster, one is Ultraman dozen Big Boss.

//Common method Overloadingpublic void PK (String name) {
/*
* This means: The amount of blood in the parameter = the amount of blood in the parameter-the energy being attacked. For example: A1. PK ("Little Monster");
* That is: The Small monster's blood volume = The Small Monster's blood volume-Ultraman's attack energy
*/
This.blood-= attack;
SYSTEM.OUT.PRINTLN (name + "attacking" + THIS.name + "," + this.name
+ "Residual blood amount is:" + This.blood);
}
Common Method Method overloading
public void PK (Boss B) {
B.blood-= attack;
SYSTEM.OUT.PRINTLN (name + "attacking" + B.name + "," +b.name + "the remaining amount of blood is:"
+ B.blood);
} Public Static voidMain (string[] args) {boss A1=NewBoss ("Ultraman", 200, 5); Boss A2=NewBoss ("Little Monster", 100, 3); Boss M1=NewBoss ("Big Boss", 100, 3); while(A1.blood > 0 && a2.blood > 0) {A2. PK (Ultraman); if(A1.blood <= 0) {System.out.println (A2.name+ "Victory!" "); Break; } A1. PK ("Little Monster."); if(A2.blood <= 0) {System.out.println (A1.name+ "Victory!" "); Break; } } if(A1.blood > 0) { while(A1.blood > 0 && m1.blood > 0) {A1. PK (M1); if(M1.blood <= 0) {System.out.println (A1.name+ "Victory!" "); Break; } M1. PK (A1); if(A1.blood <= 0) {System.out.println (M1.name+ "Victory!" "); Break; } } }

Results of the operation:

Method Construction and method overloading of Ultraman and the Battle of the Big Boss

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.