Java learns 11 (classes and objects) from scratch, and java learns objects from scratch

Source: Internet
Author: User

Java learns 11 (classes and objects) from scratch, and java learns objects from scratch
1. Face object

Ii. What is a class?

I'm sure I don't know.

In short, classes are the basic units in java, and classes are object sets with the same features and behaviors.

Iii. class Definition 3.1. class Definition class name {data type attribute ;.... Name of the Data Type method returned by public (parameter 1, parameter 2 ...) {Program Statement; [return expression;]} 3.2. class includes: attributes and methods (behavior)
Package com. pb. demo2;/** human */public class Person {// Persion is the class name/** attribute, name, sex, age */public String name; // name public String sex; // gender public int age; // age/** method (behavior) * // public void eat () {System. out. println ("dinner");} // work public void work () {System. out. println ("work ");}}
4. Create an object

The object is a class instance.

Package com. pb. demo2; public class PersonTest {public static void main (String [] args) {// create the Person class Object Person person = new Person (); // call the Person class method person. eat (); person. work ();}}
5. Parameter Methods
Package com. pb. demo2;/** human */public class Person {// Persion is the class name/** attribute, name, sex, age */public String name; // name public String sex; // gender public int age; // age/** method (behavior) * // public void eat (String name) {// input a String type parameter System. out. println (this. name + "invite" + name + "Dinner Together");} // work public void work () {System. out. the work philosophy of println (name + "is to earn money and eat! ");}}

Test class

Package com. pb. demo2;/** test class */public class PersonTest {public static void main (String [] args) {// create the Person class Object Person hanbing = new Person (); hanbing. name = "HAN Bing"; // name assignment hanbing. age = 22; // age hanbing. sex = "female"; // gender hanbing. eat ("Liu dun"); hanbing. work ();}}

Result:

HAN Bing invited Liu Dun to dinner together
HAN Bing's philosophy is to earn money and eat food!

Vi. Composition of class methods

It consists of five parts: access modifier, return value type, method name, parameter list, and method body.

7. Method overloading 7.1 and method Overloading

The method name is the same, the parameter items are different, and (the access modifier and return value type) are irrelevant.

For example:

Package com. pb. demo2;/** human */public class Person {// Persion is the class name/** attribute, name, sex, age */public String name; // name public String sex; // gender public int age; // age/** method (behavior) * // public void eat (String name) {// input a String type parameter System. out. println (this. name + "invite" + name + "Dinner Together");} // work public void work () {System. out. the work philosophy of println (name + "is to earn money and eat! ") ;}// Public void work (String contect) {System. out. println (name + );}}

Test class:

Package com. pb. demo2; import java. util. principal;/** test class */public class PersonTest {public static void main (String [] args) {/* // create the Person class Object Person hanbing = new Person (); hanbing. name = "HAN Bing"; // name assignment hanbing. age = 22; // age hanbing. sex = "female"; // gender hanbing. eat ("Liu dun"); hanbing. work (); */partition input = new partition (System. in); Person guest = new Person (); System. out. println ("*********** test ***********"); System. out. println ("Enter name:"); String name = input. next (); guest. name = name; // assign System to the object name. out. println ("Enter age:"); int age = input. nextInt (); guest. age = age; // assign the age value to System. out. println ("select gender:"); System. out. println ("1, male 2, female"); switch (input. nextInt () {case 1: guest. sex = "male"; // assign break to the object gender; case 2: guest. sex = "female"; // assign break to the object gender; default: System. out. println ("input error"); return;} // call the work method guest without parameters. work (); System. out. println ("enter a new idea:"); String contect = input. next (); // call the work method guest with parameters. work (contect );}}

Result:

* ******** Test *********** enter name: Zhang San enter age: 22 select gender: 1. MALE 2 and female 3 work philosophy is to earn money and eat! Please enter a new working philosophy: earning money. The work philosophy of wife John 3 is: earning money and taking wife
8. member variables and local variables

A member variable is defined as a variable in the class and can be used in the entire class.

The variables declared by local variables in the method are valid only in this method.

When the member variable and the variable have the same name, you can use the this keyword to call the member variable. If this is not added, local variables are called by default.

9. determine which of the following is method overload?

Public void cal (int x, int y, int z ){...}

A: public int cal (int x, int y, float z) {return 0 ;}

B: public void cal (int x, int y, int z) {return 0 ;}

C: public int cal (int x, int z ){....}

D: public void cal (int x, int y, int z ){...}

Answer: A, C

Next question:

A: int f1 () {} int f1 (int ){}

B: void f1 (int a) {}int f1 (int ){}

C: void f1 () {}int f1 (int ){}

D: int f1 (int B) {}int f1 (int ){}

Answer: A, C

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.