Java Learning Lesson 07

Source: Internet
Author: User

code block

* Use {} code, called code block.


* Local code block

Defining the life cycle of a variable, defined in a local code block, is not accessible after the local code block. Variables defined in a local code block, after the code block is out, the memory is freed.

function: The main is to save memory.


A variable defined in a local code block, although scoped is local, if there is an externally nested code block, and a variable is defined before the local code block definition,

It is not possible to define variables of the same name in our local code block.

However, it is possible to define a variable with the same name as the local code block after the local code block is executed. Because the variables in the local code block are already out of scope.


* Construct code blocks

* The method appears outside the class, and each time the constructor method is called, the construction code block is first called.

* Each time an object is created, our construction code block is called once.


Role

If there are many overloaded construction methods, and each one needs to perform the same logic, then you can consider extracting the code into the construction code block to execute.

Make the code structure more concise. Enhanced maintainability. (Not much of the scene used)



* Static code block

Occurs outside of a method in a class, but is decorated with static.

* Features: Executes as the class loads.


Use

For the entire software lifecycle, you only need to execute business logic code once.

such as the operation of the database after us.

* Synchronous code block (multithreading involved)

Inherited

* Subclasses inherit the parent class, inheriting the member variables and member methods of the parent class. But they each have their own member variables, so their values are not inherited.

* Subclasses are not inheritable for private member variables and private methods in the parent class.


* Inheritance Benefits:

Improved reusability of the code

Improved Code maintainability


* Inheritance Disadvantages:

The coupling of the class is enhanced.

* Principles of development:

Cohesion Poly, low coupling.

* Coupling: The relationship between classes and classes

* Cohesion: The ability to accomplish something independently.


* only supports single inheritance, but can support multiple layers of inheritance

Inheritance is superimposed on each other, and when a subclass inherits, it recursively looks for inheritance in the parent class, inheriting the properties and methods of the entire hierarchy until the root class.


* Cannot be inherited for a construction method.

However, there are ways to invoke the parent class's constructor method.


* Access member variables in an inheritance relationship:

* Member variables with different names

Subclasses can directly access and use non-private member variables inherited by the parent class


* Member variables with the same name

* Priority principle: If a child class defines a member variable with the same name in the parent class, the variable of the child class is given precedence

If you want to still access a variable of the same name in the parent class, we need to add the Super keyword to get it.


This : a reference to the current object, access to a variable in a subclass, or access to a variable in the parent class

Super: A reference to a parent class object; Access a variable in the parent class


* Access member methods in an inheritance relationship:

* Methods of different names

Subclasses can call methods directly to the parent class


* Method with the same name

        When a method of the same name occurs in the parent and child classes (method with the same name: refers to the return value type (parent-child class relationship is possible),         The method name and the formal parameter list received by the method are exactly the same, and the processing of the precedence principle is to call the subclass         method.

Public class extdemo {public static void main (String[] args) {Father  Father=new father (); Son son=new son (); father.name= "small head father";                 father.setsex ("male"); Father.age=29;father.speak (); Son.name= "Big head Son";                 //son.setsex ("male"); Son.age=7;son.speak ();}} Class father{string name;private string sex;int age;void setsex (String sex) {this.sex=sex;     } String getsex () {    return sex;}     void speak () {System.out.println (name+ ":" +sex+ "  " +age+ "Years);}} Class son extends father{string name;//name is a member variable with the same name as the parent class, and a member variable with a different name from the parent class;                    &Nbsp;//sex is private modified cannot directly inherit Void speak () {System.out.println (name+ ":"  +age+ "old");}} 


Son.setsex ("Male"); (error)

650) this.width=650; "Src=" Https://s1.51cto.com/oss/201710/19/24d349fc12c9ccc6f3adc0ae7bd978f5.png-wh_500x0-wm_3 -wmp_4-s_2641003699.png "title=" 9 ' mjzgocek4@}5bcr@s ' [$R. png ' alt= ' 24d349fc12c9ccc6f3adc0ae7bd978f5.png-wh_ '/>


650) this.width=650; "Src=" Https://s1.51cto.com/oss/201710/19/1198b9a08e14b526a122e0ae4a3cd866.png-wh_500x0-wm_3 -wmp_4-s_974284796.png "title=" Big Head Daddy. png "alt=" 1198b9a08e14b526a122e0ae4a3cd866.png-wh_ "/>



Override of Method

* exists in the inheritance relationship, the method defined in the subclass is exactly the same as the method in the parent class (the return value type parent-child class relationship is possible), we call the method of the subclass when we access the method through the subclass object.


* Method Rewriting considerations:

Subclasses cannot override private methods of the parent class.

When a subclass overrides a parent class method, the access permissions provided cannot be lower.

Subclasses override the parent class method, and if the parent class is a static method, the subclass must also be a static method to overwrite successfully, that is, rewrite.


* Method Overloading:

In the same class, if we have multiple methods of the same method name, but their parameter lists are different, then this way we are called overloads of the methods. At the time of invocation, the JVM can use different parameters to distinguish which method we call.


* About return value constraints for method overrides and method overloads:

Method overloading: Only the return value is different and cannot be overloaded. The parameter list must be different.

Method overrides: The return value type (parent-child class relationship is possible) requires that the return value type also be the same.

Practice:

Square, Rectangular

public class Squaredemo {public static void main (string[] args) {square square=new square (); Square.a=3;square.write (); Rectangle rectangle=new Rectangle (); Rectangle.a=3;rectangle.h=5;rectangle.write ();}}     class square{int A; int c; int S;int GetC () {c= (4*a); return c;} int GetS () {s= (a*a); return s;}    void Write () {GetC (); GetS (); System.out.println ("The circumference of the square is:" +c); System.out.println ("The area of the square is:" +s);}} Class Rectangle extends Square{int h;//method overrides int GetC () {c= (a+h); return c;} int GetS () {s= (a*h); return s;}    void Write () {GetC (); GetS (); System.out.println ("The circumference of the rectangle is:" +c); System.out.println ("The area of the rectangle is:" +s);}}


650) this.width=650; "Src=" Https://s1.51cto.com/oss/201710/20/fb2c0be63d14a21985816ebb45c8fe7b.png-wh_500x0-wm_3 -wmp_4-s_1549450642.png "title=" Squre.png "alt=" Fb2c0be63d14a21985816ebb45c8fe7b.png-wh_ "/>

* Call to construct method in inheritance

* When creating sub-class objects, it is important to create objects of the parent class first. Because you want to create the parent class object, you need to call the constructor of the parent class.

* For the first line of the constructor method for all of our classes, the first line is not added to our own this (...) or Super (...) will help us to add the default

* SUPER ()

If the default construct does not exist in the parent class and the subclass is still creating the object, then the subclass needs to display which parent class object is called to ensure the success of the parent object creation.



Be sure to make sure that the parent object is created successfully.

The first line of the constructor method, if we add the This (...) that we called, or super (...), the system will not add super () to our default.

Our This (...) or super (...) must be placed on the first line. There can only be one.


Final keyword


A.final Modified Class

Final if a class is decorated, the class cannot be inherited.


B.final member variables for decorated classes

The value of the member variable cannot be modified.

Question: So when do you initialize this value?


Can be before an object is created.

A. can be initialized in the construction code block

B. Can be initialized in the construction method

C. When defining member variables, you can initialize them directly.


Note: These three ways can only be used at the same time.


In most scenarios, our final member variables are used in conjunction with public static.


Constant classes, variable names are capitalized, and the words must be separated by _.

Class a{

public static final Double PI = 3.14;

public static final Double rate = 2.86;

}


A.pi

A.rate


Java Learning Lesson 07

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.