Java Learning notes (5)

Source: Internet
Author: User
Tags control characters data structures instance method modifier object serialization serialization stringbuffer
fifth chapter Object-oriented Foundation first, the topic 1. What is an object. How to create an object.

Classes are one of the most reusable data types in object-oriented languages, and variables declared by a class are called objects.
Create an object that includes two steps for declaring objects and allocating memory for declared objects
Let's say we wrote the following simple statement in the function:
StringBuffer str = new StringBuffer ("Hello World");
Although this statement is simple, it actually contains the following three steps:
First, Newstringbuffer ("Helloworld") applied for a heap of memory in the heap, putting the StringBuffer object into the building.
Second, Stringbufferstr declares a pointer. The pointer itself is stored on the stack (because the statement is written in a function) and can be used to point to an object of a stringbuffer type. Alternatively, this pointer can be used to hold the address of a StringBuffer object.
Finally, the equals sign (the assignment symbol) associates the two, which is to save the address of the heap of memory just applied to the value of Str. 2. What is the construction method. What are the characteristics of the construction method?

A constructor is a class method in a class method that has the same method name as the class name, and is used to automatically invoke and initialize the new object when an object is created with the new keyword.
Features of the construction method:
1 construction method is the same as the class name
2 The constructor method does not return a value and does not define a return type
3 The constructor method can be overloaded to define multiple constructed methods with the same name for the number and type of arguments
4 The constructor method can inherit, the subclass can inherit the constructor method of the parent class
5 The constructor method cannot be called explicitly directly by the programmer, which is invoked automatically by the new operator when the object is created
6 The constructor method is used only to initialize the class instance in order to simplify the initialization operation
7 Each class has at least one construction method, and if the programmer does not define a construction method for the class, the system automatically generates a default constructor for the class.
In Java, the creation and initialization of an object is uniform-both are indispensable. 3. What is the encapsulation of the class. How to set the access rights of member variables and methods to the purpose of data encapsulation.

The so-called encapsulation performance in the following areas
1 Set permissions on the class's definition to access the member variables and methods in the object.
2) Provide a unified method for other classes of reference.
3 other objects cannot directly modify the properties and methods owned by this object.
Four access rights to declare object variables and methods by using the access control character (public, protected, private, default) 4. Subclasses can inherit which member variables and methods of the parent class.

Java only supports single inheritance. Subclasses inherit properties and methods from the parent class, or they can build their own properties and methods that the parent class does not have. The inheritance of subclasses is divided into two cases due to the restriction of access control characters:
1 The subclass and the parent class are in the same package, the subclass naturally inherits the member variable that is not private in its parent class as its own member variable, and naturally inherits the method that is not private in the parent class as its own method, and the access rights of the integrated member variables or methods remain unchanged.
2 the subclass and the parent class are not in the same package, and the subclass can only inherit the protected from the parent class, the member variables and methods decorated by the public access controller. And at this point the subclass cannot inherit the friendly variables and friendly methods of the parent class.
For a member variable that the subclass can inherit from the parent class, the subclass hides the inherited member variable as long as the member variable defined in the subclass has the same name as the member variable in the parent class. For inherited methods, subclasses can hide inherited methods through method overrides. 5. Programming, given the radius and height of the circle, calculate the volume of the cylinder, using the form of reference object method to express.

See Xiti5_5.java

Package xiti5;
Import Java.util.Scanner;

Class cylinder{//Cylinder class
double r=1,height=1,vol;//member property
void Cylinder () {//constructor method

}
void Set_r (double rs) { Member Method
    r=rs;
}
void Set_height (Double h) {
    height=h;
}
Double Volume () {
    vol=3.14*r*r*height;
    Return vol;
}

}
public class Xiti5_5 {
//coding, given the radius and height of a circle, the volume of the cylinder is computed, expressed in the form of a reference object method. Public
static void Main (string[] args) {
    //TODO auto-generated method stub
    Scanner Scanner = new Scanner ( system.in);
    Cylinder Cyl=new cylinder ()//object declaration and allocating memory
    System.out.println ("The volume of the cylinder is not modified:" +cyl.volume ());//Call Method
    Cyl.set _r (ten);
    Cyl.set_height (ten); 
    System.out.println ("The volume of the modified cylinder is:" +cyl.volume ());
}

6. Supplementary reading:

"Object-oriented Analysis and Design (3rd edition)" Grady Booch
8. Design a class named Fan to represent a Fan. The class contains:
Three constants named Slow, MEDIUM, and FAST with values 1, 2, and 3 to
Denote the fan speed.
A private int data field named speed that specifies the speed of the fan (default slow).
A Private boolean data field named on this specifies whether the fan is on (default false).
A private Double data field named RADIUS that specifies the radius of the fan (default 5).
A string data field named color that specifies the color of the fan (default blue).
The accessor and mutator methods for all four data fields.
A No-arg constructor that creates a default fan.
A method named ToString () is returns a string description for the fan.
If The fan is on, the method returns the fan speed, color, and radius in
One combined string. If The fan is isn't on, the method returns fan color
and radius along with the string ' fan is off ' in one combined string.
Implement the class. Write a test program that creates two Fan objects. Assign maximum speed, radius, color yellow, and turn it on to the. Assign medium speed, radius 5, color blue, and turn it off to the second object.
Display the objects by invoking their toString () method.Second, discuss

With the development of the function of computer hardware, people's application and improvement of language make the object-oriented language come into being, it makes it possible to write the program code that is easy to maintain, easily expand and reuse, and it will be more consistent with people's thinking habits. Java is a pure object-oriented programming language.
Object-Oriented programming methods are:
Object = (algorithm + data structure)
Program = (Object + object + ...) )
Algorithm and data structure is a whole, algorithm inseparable from data structure, the algorithm can only be applied to specific data structures. 1, the four characteristics of object-oriented language: Abstraction, encapsulation, inheritance, polymorphism. 1.1, Abstract: From the concrete examples of the common nature of the formation of a general concept. 1.2, Encapsulation: In object-oriented programming design method, encapsulation (English: encapsulation) refers to the abstract function of the implementation of the details of the part of the packaging, hiding the method.

Encapsulation Performance in the following ways sets the permissions on the class's definition to access the member variables and methods in the object. provides a way to unify references to other classes. Other objects cannot directly modify the properties and methods that are owned by this object.

It is mainly through the access control character (public, protected, private, default) to implement, the following table:
1.3, Inheritance: The object of the subclass has all the properties and services of its parent class, called the subclass inheritance of the parent class. Java is a single inheritance mechanism.

The inheritance relationship between classes is a direct simulation of genetic relationships in the real world, which represents the intrinsic relationship between classes and the sharing of attributes and operations, that is, subclasses can follow certain characteristics of the parent class (inherited classes). Subclasses can also have their own independent properties and operations.

Subclasses inherit from the parent class with two main aspects:
(1) Inheritance of the property. For example, a company is a parent class, and a company has a name, address, manager, employee, etc., which are structural aspects.
(2) The inheritance of the method. A parent class defines operations such as project development, profit creation, manager appointment, employee recruitment, and so on, and the subsidiaries will inherit these actions.

The inheritance of subclasses is divided into two different situations:
1 The subclass and the parent class are in the same package, the subclass naturally inherits the member variable that is not private in its parent class as its own member variable, and naturally inherits the method that is not private in the parent class as its own method, and the access rights of the integrated member variables or methods remain unchanged.
2 the subclass and the parent class are not in the same package, and the subclass can only inherit the protected from the parent class, the member variables and methods decorated by the public access controller. And at this point the subclass cannot inherit the friendly variables and friendly methods of the parent class.
For a member variable that the subclass can inherit from the parent class, the subclass hides the inherited member variable as long as the member variable defined in the subclass has the same name as the member variable in the parent class. For inherited methods, subclasses can hide inherited methods through method overrides.

In particular, the inheritance of constructive methods should follow the following principles
1 subclasses can unconditionally inherit the constructor method without parameters of the parent class
2 If the subclass does not have a constructor method, it inherits the parent class parameterless construction method as its own construction method. If a subclass has a constructor, then when the subclass object is created, the inherited parent class is executed first, and then its own construction method is executed.
3 for constructors that have parameters in the parent class, subclasses can be referenced by using the Super keyword in their own constructor, and must be the first statement in the subclass construction method. 1.4, Polymorphism:

Narrowly defined: Because the parent class and the subclass can have methods with the same name, the JVM finds, at run time, the method of deciding which version to execute, based on the number and type of parameters of the method, called polymorphism.
Of course, this is just a type of polymorphism, that is, the polymorphic, overloaded (overload) of the operation, which behaves as if there are multiple operations with the same name, but these operations must receive a different message type. There is also a polymorphism associated with inheritance, that is, overriding (Overrride), which means that the same operation can produce different behavior when invoked by different types of objects. 2. Classes and Objects

Class encapsulates the state and method of a class of objects and is used to define the template for an object. A variable declared by a class is called an object. When a class is used to create an object, it is also called an instance of the class. 2.1. Concepts and creation of classes and objects

Class-Defined format:
[class modifier] class    class name
[extends      parent class name]
[Implements      interface name] {
    constructor method definition (can be implied);
    member variable definition; c12/> method definition;
The constructor method is a class method with the same method name as the class name in a class method, which is used to automatically invoke and initialize the new object when an object is created with the new keyword.


Create class object general form:
class name   object variable = new  class name (parameter 1, parameter 2, ...);
   or
class name  object variable;
object variable = new class name (parameter 1, parameter 2, ...);

Create an object that includes two steps for declaring objects and allocating memory for declared objects.

The difference between an instance variable and a class variable is that the class variable (static variable: static modifier) is the data variable associated with the class, all members are shared, and one member changes its value to affect other members, similar to the shared resources in the operating system. An instance variable is simply a variable associated with the corresponding object, and the instance variables of the different objects are not the same. In addition, the former can be accessed through class names and objects, which can only be accessed through objects.

Variable types are basically:
Static: Statically variable (class variable), relative to instance variable
Final: Constants
Transient: Temporary variables, for object archiving, for object serialization, see the serialization of Objects Section
Volatile: contribution variable, for sharing concurrent threads

◇ Method declaration
A method declaration includes a method name, a return type, and an external parameter. The type of the parameter can be either a simple data type or a composite data type (also known as a reference data type). 2.2. Parameter Transfer value

This is a little dizzy, just know. The value of a parameter variable in a method is a copy of the caller's specified value, and changing the value in the method does not affect its original value, and a reference must be introduced to change the original value.
For simple data types, Java implements value passing, which receives the values of parameters, but does not change the values of those parameters. If you want to change the value of the parameter, use the reference data type, because the reference data type is passed to the method by the address of the data in memory, and the manipulation of the data in the method can change the value of the data.
Extracted from the Great God:
1, the object is passed by reference
2, the Java application has and only one kind of parameter passing mechanism, namely passes by the value
3. Passing by value means that when a parameter is passed to a function, the function receives a copy of the original value
4. Passing by reference means that when a parameter is passed to a function, the function receives the memory address of the original value, not a copy of the value. Yes, I did.

Doubts about string types are not resolved.
Three words to sum up:
1. The object is to pass the reference
2. The original type is the transfer value
3.String and other immutable types because they do not provide their own modified functions, each operation is new to an object, so special treatment. It can be thought of as a pass value. 2.3, the combination of objects

We can use other objects in the definition of a new class. This is the combination (composition). Combination is one of the basic means of implementing program complex (Reusibility) in Java.
A function-like nesting that uses an object as a member of its own object, which is a member of an object nested within it.
See the Big Guy blog for details:
Relationships between Java objects (dependencies, associations, combinations, aggregations) 3, keyword 3.1, Static

The static keyword can be used to declare class variables and class methods (static methods). The difference between the two is the instance variable and the instance method.
(1) class variables and instance variables: class variables can be accessed through class names, and objects declared by the class share class variables.
(2) Class methods and instance methods: Class methods can be accessed through class names, and objects declared by the class share class methods. See examples of pictures:

Big guy Blog through Train: Java keyword final, static, abstract use summary:
http://blog.csdn.net/u013842976/article/details/51362565
The difference between Java modifier abstract,static,final:
Http://www.jb51.net/article/41430.htm 3.2, this, super

You should add the this reference when you want to specify in a class the variables or functions that use the object itself. In short, different parameters, similar to polymorphic, are used as criteria for distinguishing between the name of the instance member variable and the name of the local variable, which must be combined with this. This does not occur in class methods, because class methods can be called directly through the class name, and there may not be any objects generated.
1 represents the object created by using the constructor when it appears in the constructor method of the class;
2 represents the current object that is calling the method when it appears in the instance method.
For example:

Class a{
    int x;
    static int y;
    void F () {
        x=100;
        y=200
    }
}
in instance method F of Class A, a this,this represents the current object that uses F. So, this.x represents the variable x of the current object, and when the variable calls method F, assigns 100 to the variable x.

In turn, subclasses can hide member variables and methods inherited from the parent class, and you can use the keyword super if you want to use a member variable or method that is hidden by the quilt class in the subclass.
1 use super to invoke the constructor method of the parent class, super must be the first statement in the subclass constructor.
2 member variables and methods that are hidden by using the super operation, the entry address assigned by the hidden method is only visible to super.

In short, this refers to a reference to the current object, and super is a reference to the parent object inside the current object.
See the Big Guy blog for details:
http://lavasoft.blog.51cto.com/62575/18886/

http://blog.csdn.net/yu422560654/article/details/7299518 3.3, Final

Final can modify the local variables in classes, member variables, and methods.
1 Final class
The final class cannot be inherited, that is, it cannot have subclasses.
2 Final method
When you decorate a method in the parent class with final, the method does not allow subclasses to override the final method that can inherit, which is often used to terminate inheritance.
3) constant
A member variable or local variable that is modified by final is a constant. A constant is declared without a default value, so you must specify the value of the constant when declaring the constant, and you cannot change it.

Related Article

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.