Java object-oriented-classes and objects

Source: Internet
Author: User

Three main features of Java object-oriented: encapsulation, inheritance, and polymorphism. Classes and objects
All classes are reference data types. For a class definition, there are three most common members: constructors, properties, methods, three members can define multiple or 0, and when 0, an empty class is defined. The order of the definitions between the members of a class has no effect, and the members can call each other.
I. Defining classes
1, define the syntax of the class:
[modifier] class name {0 to multiple constructor definitions ...     0 to multiple Properties ... 0 to multiple methods ...} Modifier: Can be public, final class name: is a valid identifier, but from the readability of the program, the Java class name must be one or more meaningful words concatenating, each word capitalized, the other letters all lowercase, Do not use any connectors between words and words. Property: Used to define the data method that the class or instance of the class contains: A behavior feature or a feature implementation that defines the class or instance of the class, and the constructor: used to construct an instance of the class that the Java language calls the constructor with the new keyword to return an instance of the class.
A constructor is the fundamental way for a class to create objects, and if a class does not have a constructor, the class cannot create an instance. If the programmer does not write a constructor for a class, the system will provide a default constructor for the class, and once the programmer provides a constructor for a class, the system will no longer provide a constructor for that class. 2, defines the syntax format for a property:
[Modifier] Property Type property name [= default] modifier: Can be omitted, can also be public, protected, private, static, final, where public, protected, private three can only appear         One, which can also be combined with static and final to modify the property; property type: Any data type allowed by the Java language, including the base data type and the reference data type; property name: As long as it is a valid identifier, but if it is readable: The property name should be concatenating by one or more meaningful words, with the first letter lowercase, with the first letter capitalized, with all other letters lowercase, and no delimiters between words and words. Default value: Defines a property that can also define a default value
3, defines the syntax format for a method:
[Modifier] method returns a value type method name (parameter list) {//method body} modifier consisting of 0 to multiple executable statements: can be omitted, or it can be public, protected, private, static, final, abstract, Where public, protected, private three can only appear at most one, abstract and final can only appear in one of them, they can be combined with static adornment property; method return value type: The return value type can be any of the Java language allowed The data type, which includes the base type and the reference type, and if the return value type is declared, the method body must have a valid return statement that returns a variable or an expression (the variable or expression type must match the type declared here); If a method does not return Value, you must use void to declare no return value. Method Name: Basically the same as the property name rule, it is generally recommended that the method name begin with a verb in English text. Formal parameter list: used to define the acceptable number of adoption of the method, composed of 0 or more sets of "parameter type parameters name" combination, multiple sets of parameters separated by commas (,), the formal parameter type and formal parameter name between the English space. Note: There is a strict order of execution between multiple executable statements in the method body, and the statements in front of the method body are always executed first.
The statement that follows the method body is always executed after.
4,static
Static is a special keyword that can be used to modify members such as methods, properties, and so on. The members of the static modifier indicate that it is common to this class, so the properties and methods of static adornments are often referred to as class properties and class methods. Properties and methods that do not use static adornments are often referred to as instance properties, instance methods. Static members cannot access non-static members directly.
5, The syntax format for the definition constructor is as follows:
[modifier] Constructor name (parameter list) {//constructor execution body} modifier consisting of 0 to multiple executable statements: can be omitted, or it can be public, protected, private one of the constructor names: must match the class name Formal parameter list: The format of the parameter list is exactly the same as that of the definition method.
6, the generation and use of objects
The fundamental way to create an object is the constructor, which invokes the constructor of a class with the new keyword to create an instance of the class.        Building Object Mode one: Person P1; P1=new person ();
Building Object Mode Two: Person p2=new person ();        Call the Name property of the P2 and assign a value to the property p2.name= "small fish";        P2.say (P2.name); P2.say ("Call this method must specify a value for the parameter");
7, code example:
Person
class:
/*
* Define class
 * */
Public
class
person
{
    /*
* Define Attributes
     * */
Public
String name= "";
public
int age;
public
int foo;

    /*
* Definition Method
     * */
Public
void Say (String content)
    {
System.out.println (content);
    }

    /*
* Define Constructors
     * */
Public Person
()
    {
//define a Foo variable inside the constructor
int foo=0;
this.foo=6;
    }
//program run unique entry
Public
static void Main (String args[])
    {
        /*
* creation and use of objects
* The fundamental way to create an object is the constructor, which invokes a class by using the New keyword
.
* Constructs a class to create an instance of this class.
         * */
//Build Object mode one:
Person
p1;
p1=new person ();

//Build Object mode two:
Person
p2=new person ();
//Call the Name property of the P2 and assign a value to the property
/* If access permission is allowed, the methods and properties defined in the class
* Can be called from an instance of a class or class.
* A class or instance accesses a property by: Class. Property | method, or instance. Property | Method
* Static modified methods and properties that can be called either through a class or by an instance
         * 
         * */
p2.name= "small fish";
P2.say (p2.name);
P2.say ("Call this method must specify a value for the parameter");
    }
}


Ii. objects, references, and pointers
Person p=new the person (); This line represents the creation of two person instances, also known as the Person object, which is assigned to the P variable. The reference variable contains only a reference, which points to the actual object. A variable of person type is actually a reference, it is stored in memory, it points to the actual person object, and the real person object is stored in the heap memory. Assign the person object to a reference variable:

The reference variable in the stack memory does not really store the property data in the object, and the object's attribute data is actually stored in the memory. The reference variable simply points to the object in the heap's memory. When an object is created successfully, the object is saved in heap memory, and the Java program does not allow direct access to the object in the heap memory, but only through the object's reference. If the object in the heap memory does not have any variables pointing to the object, then the program will no longer be able to access the object, the object becomes garbage, and the Java garbage collection mechanism will reclaim the object and release the memory area occupied by the object. If you want to notify the garbage collection mechanism to reclaim a single exclusive, simply cut off all references to the object and the relationship between it. That is, you can assign these reference variables to null.
Third, the object's this reference
1, the object's this reference
Java provides a This keyword, the This keyword is the default reference for an object. The This keyword always points to the object that called the method.
2, Depending on where this occurs, this is the default reference for an object in two scenarios:
The constructor that
references the constructor that executes the initialization object in the method references the object that called the method
The maximum effect of the This keyword is to let a method in the class access another method or property of the class. This can represent any object, and when this appears in a method body, the object it represents is indeterminate, but its type is deterministic, the object it represents can only be the current class, and whoever calls this method, this represents who. The This reference cannot be used in the 3,static adornment method. 4, Note:
Most of the time, the normal method accesses other methods, properties without using the this prefix, but if the method has a local variable and a property with the same name, but the program needs to access this property in the method without using the this prefix.
5, code example
Dog class
public class Dog {//defines a property named foo as public int foo; Construction method Public Dog () {///In the constructor defines a foo variable/* * This reference can also be used as the default reference in the constructor, * This constructor is referenced in the constructor         The object to initialize.        * * */int foo=0; This.foo = 6;
}
    //define a Jump Method     public void jump ()      {        system.out.println ("Performing a Jump Method");     }    //defines a run method that lets the run method call the Jump Method     public void run ()     {//        dog d=new dog ();//         d.jump ();         //It's the right thing to write.         //java allows one member of an object to call another member directly, omitting the this prefix.         this.jump ();         So it can be written like this.         jump ();         SYSTEM.OUT.PRINTLN ("Executing the Run Method");     }    public static void  main (String[] args)   &NBSp; {        dog d=new dog ();         //execute the Run Method         d.run ();
All the Foo attributes for objects created with DOG will be set to 6,//So the following code will output 6 System.out.println (new Dog (). foo);
}}


Java object-oriented-classes and objects

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.