Java getting started -- (2) Facing objects (I), java getting started

Source: Internet
Author: User

Java getting started -- (2) Facing objects (I), java getting started
Key words: object, class, constructor, this, static, internal class I. object-oriented concept: divide the problem-solving installation rules into multiple independent objects, then, the problem is solved by calling the object method. Its features include encapsulation, inheritance, and polymorphism. 1. encapsulation: In the face of the core of an object, the object's attributes and behaviors are encapsulated without the need to let the outside world know the specific implementation details. This is the encapsulation idea. 2. Inheritance: mainly describes the relationship between classes. through inheritance, you can extend the functions of the original classes without re-writing the original classes. 3. Polymorphism: Allows duplicate names in a program. It indicates that attributes and methods defined in a class are inherited by other classes, they can have different data types or show different behaviors, which makes the same attribute and method have different semantics in different classes. Ii. Class and object 1. Class is the abstraction of objects. It is used to describe the common features and behaviors of a group of objects. The member variables and member methods can be defined in the class. The member variables are used to describe the characteristics of an object, also known as attributes. The member methods are used to describe the behavior of an object. How to create a class:

1 class Person {2 // defines the int type variable 3 int age; 4 // defines the speak () method 5 void speak () {6 System. out. println ("***") 7} 8} // Person class name, age is a member variable, speak () member Method
2. Object creation and use: Create: Class Name object name = new Class Name (); // instance object usage: object reference. object member. when an object is instantiated, the Java Virtual Machine automatically initializes the member variables and assigns different initial values to different types of member variables.
Table: initial values of member variables
Member variable class Initial Value Member variable class Initial Value
Byte 0 Double 0.0D
Short 0 Char NULL character, '\ u0000'
Int 0 Boolean False
Long 0L Reference data type Null
Float 0.0F    
In Java, null is a special constant. When the value of a variable is null, it indicates that the variable does not point to any object and becomes garbage collection. 3. The so-called class encapsulation means that when a class is defined, the attributes in the class are privatized, that is, the private keywords are used for modification. private attributes can only be accessed in the class where they are located. To allow external access to private properties, you need to provide some public methods that use public modifier, including the getXXX () method for obtaining properties and setXXX () method for setting properties.
1 class Student {2 private String name; // privatize the name attribute 3 private int age; // privatize the age attribute 4 // The following are public getXXX () and setXXX () method 5 public String getName () {6 return name; 7} 8 public void setName (String stuName) {9 name = stuName; 10} 11 public int getAge () {12 return age; 13} 14 public void setAge (int stuAge) {15 // The following checks the passed parameters. 16 if (stuAge <= 0) {17 System. out. println ("invalid age"); 18} else {19 age = stuAge; // assign a value to attribute 2 0} 21} 22 public void introduce () {23 System. out. println ("Hello everyone, my name is" + name + ". I am" + age + "years old! "); 24} 25} 26 public class Example01 {27 public static void main (String [] args) {28 Student stu = new Student (); 29 stu. setAge (-30); 30 stu. setName ("Li Fang"); 31 stu. introduce (); 32} 33}
3. constructor 1. constructor: A constructor is a special member of a class and is automatically called when an object is instantiated. 2. constructor features: ① The method name is the same as the class name;

② There is no declaration of the return value type before the method name;

③ A return value cannot be returned using the return statement in the method;

1 class Person {2 // constructor 3 public Person () {4 // constructor without parameters 5} 6 public Person (int age) {7 age =; // construction method with parameters 8} 9 public void speak () {10 System. out. println ("I am" + age + "years old! "); 11} 12} 13 public class Example {14 public static void main (String [] args) {15 Person p = new Person (20 ); // instantiate the Person object 16 p. speak (); 17} 18}

 

3. overload of the constructor: The method name is the same as that of the common method. You only need to change the parameter type or number of parameters. In general, the constructor usually uses public for modification. 4. Three common methods are as follows:

① The this keyword can be used to explicitly access the member variables of a class to solve the conflict with the local variable name.

1 class Person {2 int age; 3 public Person (int age) {4 this. age = age; // access member variable 5} 6 public int getAge () {7 return this. age; 8} 9}

② Call the member method using the this keyword.

③ The constructor automatically calls the constructor when instantiating an object. The constructor cannot call constructor like calling other methods in the program, however, you can use "this ([parameter 1, parameter 2…])" In a constructor ......])" To call other constructor methods.

Note:

① You can only use this in the constructor to call other constructor methods. It cannot be used in member methods.

② In the constructor, the statement that uses this to call the constructor must be in the first line and can only appear once.

③ This cannot be used in two constructor methods of a class to call each other.

5. static keyword 1. Use the static keyword to modify a member variable. This variable is used as a static variable. Static variables are shared by all instances and can be accessed in the form of "class name. Variable. Note: The static keyword can only be used to modify member variables, but not local variables. 2. static Method: add the static keyword before the method defined in the class to call a method without creating an object. You can use "class name. Method Name" for access. Note: Only static modified members can be accessed in a static method. static methods can create any object when called. 3. static code block: A code block modified with the static keyword is called a static code block. When a class is loaded, the static code block is executed. Because the class is only loaded abnormally, the static code block can only be executed once. 4. Singleton mode:
1 class Single {2 private static Single INSTANCE = new Single (); 3 private Single () {} 4 public static Single getInstance () {5 return INSTANCE; 6} 7} 8 // The preceding Singleton can be written in the following form: 9 class Single {10 private Single () {} 11 public static final Single INSTANCE = new Single ();
/* There are three modifiers before the variable name INSTANCE. Among them, public is used to allow external access to the variable directly, and static is used to allow external access.
"Class Name. variable name" is used to access the variable. final is used to prohibit external modification of the variable. */12} 13 14 class Example {15 public static void main (String [] args) {16 Single s = Single. the getInstance (); // getInstance () method is the only way to obtain the Single class instance object. The Single class is a Single class 17} 18}
The variable modified by the keyword final is a constant and its value is unchangeable. 6. Internal class: according to the position, modifier, and definition of the internal class, the internal class can be divided into member internal class, static internal class, and method internal class. 1. Syntax format for creating an internal Class Object: External class name. Internal class name variable name = new external Class Name (). new internal class ();
1 class Outer {2 private int num = 4; // defines the member variable of the class 3 // The following Code defines a member method, class 4 public void test () {5 Inner inner = new Inner (); 6 inner. show (); 7} 8 // The following Code defines a member's internal class 9 class Inner {10 void show () {11 // access the member variable 12 System of the external class in the member internal class method. out. println ("num =" + num); 13} 14} 15} 16 public class Example16 {17 public static void main (String [] args) {18 Outer outer = new Outer (); // create an external Class Object 19 outer. test (); // call te St () method 20} 21} 22 // directly create an internal class Object example 23 public class Example16 {24 public static void main (String [] args) {25 Outer. inner inner = new Outer (). inner (); // create an internal Class Object 26 inner. show (); // call the show () method 27} 28} 29 // when the internal class is declared as private, the outside world will be inaccessible.
2. Syntax format for creating static internal class objects: External class name. Internal class name variable name = new external class name. Internal Class Name ();
1 class Outer {2 private static int num = 6; // define the class member variable 3 // The following Code defines a static internal class 4 static class Inner {5 void show () {6 System. out. println ("num =" + num); 7} 8} 9} 10 public class Example16 {11 public static void main (String [] args) {12 Out. inner inner = new Out. inner (); // create an internal class object 13 inner. show (); // call the internal class method 14} 15}

Note: ① only static members of the external class can be accessed in the static internal class.

② Static members can be defined in static internal classes, but static members cannot be defined in non-static internal classes. 3. Method internal class: refers to the class defined in the member method, which can only be used in the current method. The internal class of the method can access the member variables of the external class.

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.