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