Object-oriented Java learning four

Source: Internet
Author: User

1. What is the format of the construction method? What are the precautions?
Permission modifier class name (argument list) {method body}
Precautions:
? The construction method does not return a value type. You do not need to write a return value. Because it is for building objects, the object is created, and the method executes the end.
? The construction method name must be consistent with the type.
The constructor method does not have a specific return value.


2. Can the construction method be overloaded? Can I have a return statement inside?
Can be overloaded can have return, but cannot have returns.


3, there are several ways to assign a member variable? What's the difference?
Get/set
Direct Assignment
Assigning values with a parameter constructor
Initialization
Static code block Assignment value
Default constructor Assignment

4. Write a complete person class, using the This keyword to assign values to member variables.
Testing in a test class

 Public classPerson {PrivateString name; Private intAge ;  PublicPerson (String name,intAge ) {         This. Name =name;  This. Age =Age ; }         PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }        }
 /     * *   * Write a complete person class and use the This keyword to assign values to member variables. Test in Test class *   @author   Vanguard *  */ public  class   Test01 { public  static  void   main (string[] args) {                Person P  = new  person ("Zhang San", 30 + "---" +p.getage ()); }}

5, as required, complete the following code (written in standard format) and test in the test class.
Standard format includes: Private property no parameter constructor with parametric constructor And getter Requirements method
demand one:
employee class employee

behavior: Show all member Information Method show ()

Requirement Two:
Animal class Animal
Attribute: Name Name, age
Behavior: Eating eat, sleeping sleep

Three requirements:
Human person
Properties: Name, age, gender gender
Behavior: Learning study, sleeping sleep

/**Employee Class Employee Attribute: Name name, work ID, wage salary behavior: Show all member Information Method show () *@authorVanguard **/ Public classEmployee {PrivateString name; PrivateString Id; Private intsalary;  PublicEmployee () {} PublicEmployee (string name, String Id,intsalary) {         This. Name =name;  This. Id =Id;  This. Salary =salary; }         PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString getId () {returnId; }     Public voidsetId (String ID) {ID=ID; }     Public intgetsalary () {returnsalary; }     Public voidSetsalary (intsalary) {         This. Salary =salary; }         Public voidShow () {SYSTEM.OUT.PRINTLN (name+ "---" + Id + "---" +salary); }}

/*** Animal Class Animal attribute: name Name, age behavior: Eat eat, sleep sleeping *@authorVanguard **/ Public classAnimal {PrivateString name; Private intAge ;  PublicAnimal () {} PublicAnimal (String name,intAge ) {         This. Name =name;  This. Age =Age ; }         PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }         Public voideat () {System.out.println ("Food for the Animals"); }         Public voidsleep () {System.out.println ("Sleeping Animals"); }    }
/*** Animal Class Animal attribute: name Name, age behavior: Eat eat, sleep sleeping *@authorVanguard **/ Public classAnimal {PrivateString name; Private intAge ;  PublicAnimal () {} PublicAnimal (String name,intAge ) {         This. Name =name;  This. Age =Age ; }         PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }         Public voideat () {System.out.println ("Food for the Animals"); }         Public voidsleep () {System.out.println ("Sleeping Animals"); }    }
/*** Test class *@authorVanguard **/ Public classTest { Public Static voidMain (string[] args) {Employee em=NewEmployee ("Zhang San", "001", 5000);                Em.show (); Animal a=NewAnimal ("Little White", 2);        A.eat ();                A.sleep (); Person P=NewPerson ("John Doe", 20, "male");        P.study ();            P.sleep (); }}

Object-oriented Java learning four

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.