Java Learning Note Four: Several corners of static and a little talk about polymorphism

Source: Internet
Author: User

Recently a bit of things, not timely update notes, learn not to abandon, summarize the recent study, which is the focus of static some applications, polymorphism is a design concept, understand easier, like fire pure clear is really not an easy thing.

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

1, the first simple introduction to the definition of static. Static means that this class variable or class method does not need to be instantiated for use. A variable that uses static is a class variable and also becomes a static field (static field). These are the basic semantics of static, which is understood and continues to look down.

Java Core Technology (Volume I) there is a description for static: "If you define a variable as static, there is only one such variable for each class." ”

This sentence how to understand, the language expression is this, in a main method, the class variable (static Class) is shared by all objects (shared meaning a bit like a pointer, you manipulate one to change the value, the other values will change). This is the one class above that has only one such variable.

It is more straightforward in the specific code. I define a class for test:

 Public class test{    publicstaticint  : Age    ;  Public int A;}

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

Now, the following test class initializes 3 test objects, T1,T2,T3, and T1 's age to 10,a set to 5, so what is the age,a of T2 and the age and a of T3, respectively?

 Public class tetst{    publicstaticvoid  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);}

As a result, t1.age=t2.age=t3.age=10;

and t1.a=10;t2.a=0;t3.a=0;

Why not explain it, as mentioned above, a class variable in a main class is shared. This is also a dangerous place for class variables, try not to expose the static, it may affect the program.

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

As a result of the above bedding, directly write an example, there are Father class and inherit father son class. The structure is simple.

 Public class Father {    publicstaticint a=1;}  Public class extends father{}

Father has a static class variable A,son inherits father, so that son has a static variable a of father.

The problem now is that there are two possibilities for this static variable: Father is shared with son, or son's own private. Write the following code validation:

 Public classTest { Public Static voidMain (string[] args) {Father F1=Newfather (); Father F2=Newfather (); Father F3=Newfather (); Father F4=Newson (); Son S1=Newson (); 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=Newson (); s2.a=50;        System.out.println (s2.a);        System.out.println (s1.a);        System.out.println (f1.a);            System.out.println (f2.a); }}

What's the print? The previous line prints 100, and the next row prints 50.

As a result, inheriting a class, the static method of its parent class is still a relationship that is shared with the child class. Modifying its value, whether it is a parent or a subclass, affects the value of other objects.

3, said the static, simply say I to polymorphic learning.

Polymorphism refers to a method name, multiple behavioral results. This is a polymorphic definition.

I feel that the most central sentence of polymorphism is the realization of polymorphism.

Polymorphism is achieved by overriding (overriding) override and method overload overload. This sentence looks very dull, actually not, it contains the polymorphic most important thing.

For example, if you need to use the idea of a class now, print isosceles triangle, right triangle, and squares. This design.

First, a class of super-class graphics is designed, and he has a variable n that stores the number of lines of graphics that need to be printed, and a method for performing a print operation.

Three subcategories of isosceles triangle, right triangle, and squares inherit the superclass shape. At this point, you choose to override the print operation and write your own print operations for each subclass.

In the test class, we declare a super class graph class, can give him new different subclasses, so that you can implement a parent class printing operations to print different graphics.

Java Learning Note Four: Several corners of static and a little talk about polymorphism

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.