Qingfeng
2017/2/17
function; Understand the member methods and construction methods of a class
Class Creatways
{
public static void Main (String args[]) {
Person p1 = new Person (23, "armor");
System.out.println ("p1.age=" +p1.age);
P1.count (2,3);
P1.count (3,6);
Person p2 = new Person ("Jia Bao");
}
}
Class Person
{//attribute: member variable
int age;
String name;
Behavior: Member Methods
public void count (int num1, int num2) {
int result = 0;
result = Num1 + num2;
SYSTEM.OUT.PRINTLN (result);
}
The result is returned to the keynote function Note: 1. Return the result and return type to be consistent 2. When calling member methods and given the number of specific values, type to be consistent!
public int count1 (int num1, int num2) {
return num1+num2;
}
//Construction method
/*
primary role: Completion of initialization of objects
Features: 1. Method names and class names are consistent;
2. No return value
3. When you create a new object for a class, the system automatically calls the constructor of the class to complete the initialization of the new Object!
*/
public person (int age, String name) {
System.out.println ("Construction Method One");
This.age = age;//Note: this
name = name;
}
Public person (String name) {
System.out.println ("Construction Method II");
This. name = name;
}
}
Java member methods, constructing methods