Java Paradigm Essay

Source: Internet
Author: User

Recently in the Royal Park is very boring ah, the only thing to avoid loneliness is to stop, constantly thinking about the QWQ;

Recently do the NDK, Java a little forget, suddenly see some Java paradigm problems, hesitated for a while, think about, decided to write a note down.

Model Grammar This online search Niang can be to a lot of, I don't remember, mainly the upper and lower limit of the type of fan.

Case:

Public class Test {

Public static class base{

}

Public static class A extends base{

}

Public static class fanxin<t>{

t t;

Public T get () {   

return t;

};

Public void set (T t) {   

This . t = t;   

}

}

Public static class B extends a{

}

  Public static void Main (string[] arvg) {

fanxin<? extends base> fanxin = new fanxin<a> ();

Base a = Fanxin.get ();

//fanxin.set (New A ()); 1 compiling error

fanxin<? Super A> fanxin1 = new fanxin<base> ();

a obj = (a) fanxin1.get ();

//a obj = Fanxin1.get (); 2 Compile error

//fanxin1.set (New Base ()); 3 Compiling error

Fanxin1.set (new A ());

}

}

The first Java paradigm I understand is for security, so that you can detect the type at compile time, but not the type conversion exception that occurs when you run it. The first Fanxin.set (new A ()); The error is because, here the type is defined by the upper bound, so here the wildcard represents the type is a subclass of base or base, although a is a subclass of base, but this is not safe, consider this situation:

fanxin<b> fanxin = new fainxin<b> ();

...... Various operations ...

fanxin<? Extends base> fanxin1 = fanxinb;

Fanxin1.set (New A ());

b b = Fanxin.get ();//There's a problem here.

This time the unsafe factor has occurred, we set an object A to fanxin1, but Fanxin1 is essentially fanxin<b> object, if we fanxin.get () is actually a object, the runtime will have type conversion exception.

This is not safe, so the compile time will be error, if must set, then you can force the type conversion to do, but this also lost meaning:

Fanxin<a> Fanxina = (fanxin<a>) fanxin1;

Fanxina.set (New A ());

The second problem is that the wildcard is the parent of a or a, so we can assign a value, and the object-oriented principle is that subclasses can replace the parent class, so it is safe. But if we read the value of the zone and then assign it to other objects, then there will be problems, such as;

fanxin<base> fanxinbase = new fanxin<base> ();

fanxin<?   Super a> fanxin1 = fanxinbase; fanxin<? Super a> fanxin = new Fanxin<? Super A> (); Compile error

A = Fanxin1.get ();

Here Fanxin1.get () actually obtains is the base class, assigns the value to Class A, compiles the error, the runtime will have the type conversion exception.

Of course, if we know the type of this class, then we can also use coercion type conversion to avoid compiling errors.

Cases:

Base B = (base) fanxin1.get ();

anyway? is the wildcard inside the model, extends, super just provides the upper and lower bound information to compile, the concept of have set in the inside,fanxin<? Extends Base> the type may be any subclass of Base, so we cannot have an assignment operation because the subclass and subclass are different types, but subclasses can replace the parent class, so you can read the value by the parent class, fanxin< Super A> The type is a superclass of a, so we can assign any subclass of a to him, which is safe, but when read, it is not known what type it is, but the Java class is an extension of the object class, so you can get the value assigned to the Obj object, object obj = Fanxin.get (); If you want to assign a value to another object then only the coercion type is converted.

Java Paradigm Essay

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.