Java class and object, java object

Source: Internet
Author: User

Java class and object, java object
1. Object

An object is an entity that exists in a thing. It is usually divided into two parts: the attributes of the object and the behavior of the object.

 

2. Class

Class is the general term of a class of things. If a thing is abstracted as an object, class is the general term of this object.

Xx class ----- instantiate -------> xx object

A class is a carrier that encapsulates the attributes and behaviors of an object, and an object is a class instance.

 

3. Features of Object-oriented Programs: encapsulation, inheritance, and Polymorphism

Encapsulation

Encapsulation is the core idea of object-oriented programming. It encapsulates the attributes and behaviors of objects and hides the data implementation methods for users.

The user can only interact with the object data through the object method. The advantage of this is that when the object data mode changes

If you use the same method to operate data, other objects will not be affected.

Inheritance

Sometimes a class has a certain relationship with the class. Common examples include: dependency (uses-a), aggregation (has-a), and inheritance (is-).

The is-a relationship when the child class inherits the parent class. That is, when the child class inherits the parent class, the Child class has the attributes and methods of the parent class, and the Child class can define itself.

The unique attributes and methods can reuse the code of the parent class, shorten the development time, and improve the system performance.

Polymorphism

Polymorphism refers to the feature that applies the parent class object to sub-classes. When a method in the parent class has different implementations in the sub-classes, it is instantiated using the parent class.

When you subscribe to and call this method, you can only know the specific method to be called during running.

 

4. Class Structure

Public class Book {private String name; // member variable private String price; // member variable/*** constructor: * The constructor has the same name as the class * each class can have more than one constructor * the constructor can have 0, 1, and more than one parameter.
* The constructor has no return value. * The constructor is always called along with the new operation.
* If there is no constructor in the class, the compiler automatically creates a default constructor without parameters. However, if there is a constructor with parameters in the class, the default no-argument constructor */public Book () {} public Book (String name, String price) {super (); this. name = name; this. price = price;}/*** Method * permission modifier return value type method name (parameter type parameter name) {* // method body * return value; (Not required when the return value type is void) *} * @ return */public String getName () {return name;} public void setName (String name) {// name local variable, this is created when the method is executed and destroyed when the method ends. name = name; // this Keyword: this references the current object of the class, this. name references the member variable name} public String getPrice () {return price;} public void setPrice (String price) {this. price = price ;}}

 

5. Permission Modifier
  Public Protected Default Private
This class
Same package  
Subclass    
Other packages      

 

6. static keywords

Static can be used to modify member variables and Methods. static modified member variables are called static variables and methods are called static methods.

Static variables and methods have only one copy in the memory. They belong to Classes and are shared among objects. They are called by class name, member variables, or methods.

When a member variable is modified by static and final, it is called a static constant and a constant cannot be modified.

Because static members belong to the class, note the following:

1. Static methods cannot use the this keyword

2. Static methods cannot be called directly.

3. variables in the method body cannot be declared as static

Static also has a common usage of the Factory method:

NumberFormat per = NumberFormat.getPercentInstance();        NumberFormat cur = NumberFormat.getCurrencyInstance();        double d = 0.2;        System.out.println(per.format(d));//20%        System.out.println(cur.format(d));//¥0.20

The Numberformat class uses the factory method to generate format objects of different styles.

 

7. method parameters

When a method with parameters is called, the parameter value in the method is a copy of the parameter value.

The method cannot modify a parameter of the basic type.

The method can change the status of an object type parameter.

Methods cannot be implemented to allow object parameters to reference a new object

Public static void main (String [] args) {// 1 int I = 2; changeValue (2); // In the method, x copies the I value, which is 2, method execution ends x = 6, but the value of I remains unchanged System. out. println (I); // 2 // 2 Book B = new Book (); B. setName ("Hello"); System. out. println (B. getName (); // Hello changeBookName (B); // the book in the method copies the reference of B, so B and book point to the same object. Therefore, after the setName method is called, The name value of B changes System. out. println (B. getName (); // Hello, World // 3 Book b1 = new Book ("hello", "11"); Book b2 = new Book ("world ", "22"); // book1 and book2 in the method copy the reference of b1 and b2 respectively. After the method is run, book1 and book2 exchange the reference address, // It can only indicate that the reference objects of book1 and book2 have changed after the method is completed, and the value of switchBook (b1, b2) of b1 and b2 is not affected; System. out. println (b1.getName (); // hello System. out. println (b2.getName (); // world} public static void changeValue (int x) {x = x * 3;} public static void changeBookName (Book book) {book. setName (book. getName () + ", World");} public static void switchBook (Book book1, Book book2) {Book temp = book1; book1 = book2; book2 = temp ;}

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.