Content includes:
/************************
Packaging
Analysis of problems caused by inheritance and use of inheritance
Variable hiding and method rewriting
*************************/
I. Encapsulation
Encapsulation via get and set
Here's a tip: choose quick and Efficient methods with shortcut ALT + Shift + 3, choose get and set for quick creation, with the following code:
PackageCom.jfu.one; Public classPackage {Private intDog; PrivateString Cat; Public intGetdog () {returnDog; } Public voidSetdog (intdog) { This. Dog =Dog; } PublicString Getcat () {returnCat; } Public voidSetcat (String cat) { This. cat =Cat; } Public Static voidMain (string[] args) {}}
Two. Inheritance
Function: The reuse of code can be achieved through inheritance.
Java does not support multiple inheritance.
Subclasses can inherit the variables and methods of the parent class, except for private.
Create a subclass of a class by adding the extends clause to the declaration of the class:
class extends Superclass () { }
Examples are as follows:
Car is the parent class, Smallcar inherits car for subclasses, and BMW inherits Smallcar
Car.java
Package Carry; Public class Car { int price ; Private String color; Public Static void print () { System.out.println ("Parent Car");} }
Smallcar.java
Package Carry; Public class extends Car { void Smallcar () { System.out.println ("This one small car class"); } Public Static void Main (string[] args) { new Smallcar (); Smallcar.print (); }}
Bmw.java
Package Carry; Public class extends smallcar{ publicstaticvoid main (string[] args) { new BMW (); Bmw.print (); Bmw.smallcar (); }}
Running Bmw.java, the results are as follows:
Parent car This one small car class
Results Analysis:
In Bmw.java, this class inherits the Smallcar class, and the Smallcar class inherits from the car class, so the BMW class can use the properties and methods of the Smallcar class and car.
Three. Variable hiding in succession and method overloading principle
Variable hiding principle:
The variable name in the subclass is the same as the variable name that inherits from the parent class, and the variables that inherit from the parent class are hidden.
Method Rewrite principle:
1. Must have an inheritance relationship
2. The overridden method cannot have more restrictive access rights than the overridden method, which can be the same.
3. After rewriting the weight in the method can not be specific to the method to generate more exceptions (exceptions).
4. Return value, method name, method must have the same number of arguments as the parent class. (That is, only modify the contents of the inside, the other can not be changed!)
Animal.java
Package OverWrite; Public class Animal { publicvoid back () { System.out.println ("Animal is speaking");} }
Dog.java
Package OverWrite; Public class extends Animal { // method overrides public void back () { System.out.println ("The dog is Talking") ; } Public Static void Main (string[] args) { new Dog (); Dog.back (); }}
Execution Result:
The dog is talking.
"Learning Notes" Java Object-oriented advanced programming 1