2016/5/17 Study Record

Source: Internet
Author: User

1 A solution appears void is a invalid type for the variable case.

Void is a invalid type for the variable D means:
Variable d does not support void type
The first step: check spelling is wrong;
Step two: Check the position of the function, especially the function that has a nested relationship.

Today I contacted the inheritance of the definition of the part of the function in the main function, jumped out of such a problem, later after the investigation found the wrong position.

2 today's code, mainly to learn the inheritance, simple example, the use of the ArrayList container method.

 PackageDome;Importjava.util.ArrayList; Public classDatabase {//defines a container in which the object in items is of item type;    Privatearraylist<item> items =NewArraylist<item>();//defines the function to place the object I into the container.      Public voidAdd (Item i) {items.add (i);}//defines the list function, which allows each object in the items to print, and the print operation is defined in item     Public voidlist () { for(Item i:items) i.print ();    System.out.println (); }                             Public Static voidMain (string[] args) {//TODO auto-generated Method StubDatabase db=NewDatabase (); Db.add (NewCD ("Hello", 1)); Db.add (NewDVD ("Funny", "dog"));    Db.list (); }        }
 PackageDome; Public classItem {//Initialize the parameters of the item class    PrivateString title = "";//the Write constructor initializes the object     PublicItem (String title) { This. title=title; }//write functions, output parameters. Complete the Task     Public voidprint () {System.out.println (title); }         Public Static voidMain (string[] args) {//TODO auto-generated Method Stub                        }}
 PackageDome; Public classCdextendsItem {//Initialize Parameters    Private intnum;//write constructor, object parameter initialization     PublicCD (String T,inti) {Super(t);  This. num=i; }//Write the print function, and output the object parameters. Called when the parent class is called with Super.     Public voidprint () {Super. Print ();    SYSTEM.OUT.PRINTLN (num); }     Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//initialize the object cd; Call your own function printCD cd=NewCD ("Hello", 7);    Cd.print (); }}
 PackageDome; Public classDvdextendsItem {//Initialize Parameters    PrivateString ss= "";//define constructors, initialize variables;     PublicDVD (String title,string ss) {Super(title);  This, s1=SS; }//define the function, call the parent class in Super., output parameters;     Public voidprint () {Super. Print ();    SYSTEM.OUT.PRINTLN (ss); }         Public Static voidMain (string[] args) {//TODO auto-generated Method StubDVD DVD =NewDVD ("Hhhhh", "why");    Dvd.print (); }}

3. You can initialize the CD type Object CD, using item i = CD; the object of the subclass can be assigned directly to the object of the parent class;

Item i = CD;

I.print ();

What does it output? The previous sentence means that I and the CD manage the same object, so the latter output is to let them work together with the object to print (), so the output in the CD class output, the compiler has no error, because the print () function in the item and CD two classes have.

Add a function such as public void GetValue () to the CD class;

I.getvalue (); error, because the compiler does not know that there is getValue () this function

Cd.getvalue (); Can run

4. Db.add (CD) in the above code means that the object of the handle class is assigned to the object of the parent class. This action is called upcast upward shape.

5.

New= c; // This step is called upcastC = v; // Error This step is called cast but the compiler will error

The sentence can be modified with coercion type conversion

c = (Car) v;

6. The parent class of all classes is Object

For example in the code of the clock simulation

 PackageClock; Public classClock {intUplimit; inttick;  PublicClock (intuplimit) {         This. Uplimit =Uplimit; }     Public voidtick () {tick++; if(Tick = =uplimit) Tick= 0; }     Public intGetValue () {returntick; }     PublicString toString () {//The ToString function here is a function inside the object class, and if you change to a different function name, you cannot let the string enter the function in the display class .        if(TICK&LT;10)return"0" +tick; Else return""+tick; }         Public Static voidMain (string[] args) {//TODO auto-generated Method StubClock c=NewClock (24);  while(true) {C.tick ();    System.out.println (C.getvalue ()); }    }}

And the display code that introduced the class.

 Packageclock.a;ImportClock.clock; Public classDisplay { PublicClock hour=NewClock (24);  PublicClock minute=NewClock (60);  Public voidtick () { while(true) {minute.            Tick (); if(Minute.getvalue () ==0) hour.            Tick ();        Show (); }        }     Public voidShow () {System.out.println (hour+":"+minute);//here because of hour the + operation is to find the right is a string, automatically called the Clock class inside the ToString () function to convert it to a string.    Otherwise two reference variables are directly output//reference type. }     Public Static voidMain (string[] args) {//TODO auto-generated Method StubDisplay c=NewDisplay ();            C.tick (); }}

Here, ToString is inside the object parent class.

A 7.Polymorphism polymorphic is a subclass that allows an object to be assigned to a parent class. The pointer is actually pointing to the same position.

2016/5/17 Study Record

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.