Java study NOTE 4: Several corners of static and small talk about polymorphism, learning notes static

Source: Internet
Author: User

Java study NOTE 4: Several corners of static and small talk about polymorphism, learning notes static

 
 

There are a few things recently. If you haven't updated your notes in time, you can't discard your studies. To sum up the recent studies, the focus is on some static applications. polymorphism is a design concept that is easy to understand, it is not easy to think about it.

For static, there are two main points that I learned.

1. First, let's briefly introduce the definition of static. Static means static, that is, this class variable or class method can be used without instantiation. Static variables are class variables and become static fields ). These are the basic semantics of static. After learning about them, let's continue to look down.

Java core technology (Volume 1) has a description of static: "If you define a variable as static, each class has only one such variable ."

How can this sentence be understood? The language expression is like this. In a main method, class variables (static classes) are shared by all objects (sharing means like pointers, if you change one of the values, other values will change ). This means that the class mentioned above has only one such variable.

It is more direct in the specific code. I define a Test class:

public class Test{    public static int age;    public int a;}

In the Test class, there is a static int variable age and a non-static variable.

Now, three Test objects are initialized in the following Test classes, t1, t2, and t3, respectively. The age of t1 is set to 10, and a is set to 5, so the age of t2, what are age and a of a and t3?

public class TetsT{    public static void main(String[] args){        Test t1=newTest();        Test t2=new Test();        Test t3=new Test();        t1.age=10;        t1.a=5;                system.out.print(t1.age+"    "+t2.age+"     "+t3.age);        system.out.print(t1.a+"    "+t2.a+"       "+t3.a);}

The result is t1.age = t2.age = t3.age = 10;

T1.a = 10; t2.a = 0; t3.a = 0;

You don't need to explain the reason. As mentioned above, class variables in a class in a main are shared. This is also a dangerous place for class variables. Do not expose static variables as much as possible, which may affect the program.

2. What is the result of static variables in inheritance?

With the above preparations, we can directly write an example, including the father class and the son class that inherits the father class. The structure is simple.

public class father {    public static int a=1;}public class son extends father{}

Father has static class variable a, and son inherits father, so son has a static father variable.

The problem is that there are two possible static variables: father and son, or son's own private one. Write the following code for verification:

public class test {    public static void main(String[] args) {        father f1 = new father();        father f2 = new father();        father f3 = new father();        father f4 = new son();        son s1 = new son();                f1.a=100;                System.out.println(f1.a);        System.out.println(f2.a);        System.out.println(f3.a);        System.out.println(s1.a);                son s2=new son();        s2.a=50;        System.out.println(s2.a);        System.out.println(s1.a);        System.out.println(f1.a);        System.out.println(f2.a);            }}

What is printed? 100 is printed in the previous row and 50 is printed in the next row.

As a result, the static method of the inherited class and its parent class is still shared with the subclass. Changing the value of a parent class or subclass affects the values of other objects.

3. After static, let me briefly explain how to learn polymorphism.

Polymorphism refers to one method name and multiple behavior results. This is the definition of polymorphism.

I feel that the core of polymorphism is the implementation of polymorphism.

Polymorphism is implemented by overwriting override and method overload. This sentence looks dull, but it does not actually contain the most important thing about polymorphism.

For example, if you want to use the class idea, print the isosceles triangle, right triangle, and square. This design.

First, design a super-class graphics class. It has a variable n, which is used to store the number of graph lines to be printed. There is also a method to execute the print operation.

The three sub-classes of the isosceles triangle, right triangle, and square inherit from the superclass. In this case, rewrite the print operation to write your own print operations for each subclass.

In the test class, we declare a super-class graphics class, and we can give it new to different subclasses, so that we can implement a print operation for the parent class to print different images separately.

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.