Java member methods, constructing methods

Source: Internet
Author: User

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

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.