Java learning notes-inheritance and polymorphism (lower), java learning notes
1. Develop superclass through inheritance)
2. Use the super keyword to evoke the constructor of superclasses
3. overwrite the method in the superclass
4. differentiate override and overload
5. Explore the toString () Class in the Object class
6. Discovery of polymorphism and dynamic binding
7. Explain why downward transformation is necessary
8. Explore the equals method in the Object class
9. storage, retrieval, and implementation of ArrayList objects
10. Use the ArrayList class to implement Stack
11. Use data and methods in the superclass, protected
12. Use the final module to disable coverage of classes and Methods
9. storage, retrieval, and implementation of ArrayList objects
ArrayList class, which is useful for storing objects. You can create an array to store objects. However, once an array is created, its size is fixed. Java provides the ArrayList class to store unlimited objects.
ArrayList Method Area
| ArrayList () |
Create an empty class |
| Add (o: Object): void |
Add an element to the end of the list |
| Add (index: int, o: Object): void |
Add objects to a special position in the list |
| Clear (): void |
Clear list |
| Contains (o: Object): boolean |
Include an object? |
| Get (index: int): Object |
Returns the element at a certain position. |
| IndexOf (o: Object): int |
Returns the position of an element. |
| IsEmpty (): boolean |
List of null values |
| LastIndexOf (o: Object): int |
Location of the last object |
| Remove (o: Object): boolean |
Match the element and remove it from the table |
| Size (): int |
Table size |
| Set (index: int, o: Object): Object |
Set elements at a certain position |
TestArrayList. java
public class TestArrayList { public static void main(String[] args){ java.util.ArrayList cityList = new java.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(int i = cityList.size()-1 ;i>=0;i--) System.out.println(cityList.get(i)+" "); }}
The result is as follows:
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 reports an error when no type conversion is performed.
For more information about type conversion, see previous notes.
10. Use the ArrayList class to implement Stack
With regard to Stack usage, Stack is also a linear table, but this linear table is quite special and can only be inserted and popped at the end of the table, and the above example is changed, pushed and pop, you only need to call the corresponding method in the push method.
Data Region
Save object
Size
Method Area
Push: Find the end of the team and insert it.
Pop: locate the team end, get (index: int): Object, and delete the team end + remove (index: int): boolean
GetTop: Follow the method of the ArrayList class to modify it.
11. Use data and methods in the superclass, protected
If you want a subclass to obtain the data and methods of the parent class, non-Child classes cannot access the data and methods.
Public private protected
Public region blocks can be modified. The pirvate region block subclass cannot be accessed directly. The protected region blocks are visible but cannot be changed.
Visibility of data and methods
| |
Other classes |
Same package |
Subclass |
Different packages |
| Public |
True |
True |
True |
True |
| Protected |
True |
True |
True |
-- |
| Defaul |
True |
True |
-- |
-- |
| Private |
True |
-- |
-- |
-- |
12. Use the final module to disable coverage of classes and Methods
Final keywords
public fina class c{.....}
public class Test{ public final void main(){ ....... }}