Java Learning notes-inheritance and polymorphism (bottom)

Source: Internet
Author: User

1. Using inheritance to develop superclass (superclass)

2. Use super keyword to evoke super class construction method

3. Overriding methods in Super-class

4. Distinguish between override and overload

5. Explore the ToString () class in the object class

6. Discovery of polymorphism and dynamic binding

7. Describe why it is necessary to interpret the downward transition

8. Explore the Equals method in the object class

9. Storage, retrieval, implementation of ArrayList objects

10. Using the ArrayList class to implement the stack

11. Using data and methods in super-class, protected

12. Using the final module to suppress overrides for classes and methods

9. Storage, retrieval, implementation of ArrayList objects

ArrayList class, which is useful for storing objects, you can create arrays to store objects, but once the array has been created, it is already fixed in size. Java provides the ArrayList class for storing an unlimited amount of objects.

ArrayList Method Area

ArrayList () To create an empty class
Add (o:object): void Add an element at the end of the list
Add (Index:int, o:object): void Add an object to a particular place in the list
Clear (): void Empty the list
Contains (O:object): Boolean Whether to include an object
Get (Index:int): Object Returns the element at a location
IndexOf (o:object): int Returns the position of an element
IsEmpty (): Boolean Determines whether the list is empty
LastIndexOf (o:object): int The position of the last object
Remove (O:object): Boolean Match elements and remove them from the table
Size (): int Size of table
Set (Index:int, o:object): Object Set the element at a location

Testarraylist.java

 Public classTestarraylist { Public Static voidMain (string[] args) {java.util.ArrayList citylist=Newjava.util.ArrayList (); Citylist.add ("Beijing"); Citylist.add ("Shanghai"); Citylist.add ("Tianjin"); Citylist.add ("Xiamen"); Citylist.add ("Fuzhou"); System.out.println ("List Size?" +citylist.size ()); System.out.println ("Is Guangzhou in the list? The answer is "+citylist.contains (" Guangzhou ")); System.out.println ("The location of Shanghai in the list?" The answer is "+citylist.indexof (" Shanghai ")); System.out.println ("Is the list is empty? The answer is "+citylist.isempty ()); Citylist.add (2, "Hangzhou"); Citylist.remove ("Tianjin"); Citylist.remove (1);                System.out.println (Citylist.tostring ());  for(inti = Citylist.size ()-1; i>=0;i--) System.out.println (Citylist.get (i)+" "); }}

The results are shown below

This object is used as a string array, if the object is stored?

Add the following code

List.add (new Circle (1, 2.5)), List.add (new Circle (2, 15.5 for (int i = 0; i<2;i++)   System.out.println ("The area of the circle" + (circle) list.get (i)). Number () + ' is + ' + (circle) List.get (i). Getarea ());

The compiler has an error when the type conversion is not done.

You can refer to the previous notes for type conversions.

10. Using the ArrayList class to implement the stack

On the use of stacks, stack stack is also a linear table, but this linear table is very special, only at the end of the insert and pop-up, the above example changes, push and pop, as long as the push method to call the corresponding method.

Data region

Save Object

Size

Method area

Push: Find the end of the team and insert

Pop: Find the end of the queue, make a Get (Index:int): Object and delete the end of the queue +remove (Index:int): Boolean

GetTop: According to ArrayList class method modification can

11. Using data and methods in super-class, protected

If you want subclasses to get the data and methods of the parent class, non-subclasses cannot access the data and methods.

The public private protected the previous study

Public block blocks can be modified, pirvate block sub-classes can not be directly accessed, protected area block visible but cannot be changed.

Visibility of data and methods

Other classes Same package Sub-class Different packages
Public True True True True
Protected True True True --
Defaul True True -- --
Private True -- -- --

12. Using the final module to suppress overrides for classes and methods

Final keyword

  

 Public class c{...}
 Public class test{  publicfinal  void  main () {          ...    }}

Java Learning notes-inheritance and polymorphism (bottom)

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.