Java Construction methods

Source: Internet
Author: User

Complete the construction of the image, create the time for the image to use, new, initialize the image with:

Student str=new Student ();

Each class has a default parameterless construction method,

Construction method does not return a value

The construction method is the same as the class name

Public class name () {}

A direct tune between the construction methods, this(); wrote the first sentence in this method

You can assign values to the properties of the image when constructing a pair of images

With ginseng ( with reference )

GOUZHAOFANGFA p1=New gouzhaofangfa ("Han Meimei");

Instance:

1  Packagecom.liu.u6.copy1;2 /*3 * Please construct a constructor method for a class4  */5  Public classGOUZHAOFANGFA {6     //define member variables, properties of a class7 String name;8 String sex;9     intAge ;Ten     //creating a parameterless construction method One      PublicGouzhaofangfa () { A          This. name= "Korean three"; -     } -     //To Create a parameter construction method the      PublicGouzhaofangfa (String name) { -          This. name=name; -     } -     //Create an argument-free method +      Public voidWork () { -System.out.println ( ThisName+ "Work philosophy is: work has food to eat"); +     } A     //creating a method with parameters at      Public voidWork (String context) { -         //Defining local Variables -         intAge= This. Age; -System.out.println ( This. name+age+ "Years of work philosophy is" +context); -     } - } in  -  Packagecom.liu.u6.copy1; to ImportJava.util.Scanner; +  Public classTESTGOUZHAOFANGFA { -      Public Static voidMain (string[] args) { the         /*//Create a new pair of images * gouzhaofangfa p1=new gouzhaofangfa (); $ p1.name= "Zhang Meimei";*/Panax Notoginseng         //Select a construction method with parameters to create a pair of image -GOUZHAOFANGFA p1=NewGouzhaofangfa ("Han Meimei"); the         //P1.name Here is the this.name member variable +System.out.println ("Welcome" +p1.name+ "Come to the global village!" "); AScanner input=NewScanner (system.in); theSystem.out.print ("Please Enter your gender: (1: Male 2: female)"); +         intWant=input.nextint (); -         Switch(Want) { $          Case1: $p1.sex= "Male"; -              Break; -          Case2: thep1.sex= "Female"; -              Break;Wuyi         default: theSYSTEM.OUT.PRINTLN ("Operation Error! "); -              Break; Wu         } -System.out.print ("Please enter your Age:"); About         //assigning an Age value $P1.age=input.nextint (); -         //calling the parameterless work method - p1.work (); -System.out.print ("Please enter work concept:"); AString lixiang=Input.next (); +         //calling the parametric work method the p1.work (Lixiang); -System.out.println ("**********************"); $      the     } the}

Output Result:

Java Construction 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.