/*final (final, modifier) final refers to the variable in the stack memory, not the value in the heap memory cannot be used for the final keyword: when the 1. final keyword modifies a basic type of variable, The variable cannot be re-assigned, and the first value is final. When the 2. FIANL keyword modifies a reference-type variable, the variable cannot be re-directed to the new object. The function cannot be overridden when the 3. final keyword modifies a function. When the 4. final keyword modifies a class, the class cannot be inherited. 5. is final modified member variable, must give the initial value constant modifier is generally: public static final *///round class circle{double r; //radius public static final double pi = 3.14; // Fixed invariant public circle (double r) {this.r = r;} Calculate Area Public final void getarea () {System.out.println ("the area of the circle is:" +r*r*pi);}} Class demo2 extends circle{public demo2 (Double r) {super (R);} Public static void main (String[] args) {final circle c = new circle (4.0); Test (c); //will not error DEMO2 C = NEW DEMO2 (4.0); C.getarea ();} Public static void test (circle c) {c = new circle (5.0); // The C variable again points to the newLike. C.getarea ();}}
/****: Current Problems:1. The run method of the animal class is not described correctly. 2. does not have to be mandatory subclasses must override the Run method. Image class Application Scenario: When we describe a class of things, we find that there is a certain kind of behavior, but this behavior is not specific at present, then we can take the statement of this behavior, but not to achieve this behavior, when this behavior we call the act of the elephant, we need to use Like class. Like the benefits of the: mandatory subclass must implement the specified method. The details of the like class:1. if a function does not have a method body, the function must be decorated with an abstract modifier, which modifies the function as a function of the * * . 2. if a class appears with a function like * * *, the class must also use the abstract adornment. 3. if a non-* * * * * * * * * * * * * * Inherits the image class, then all the image methods of the * * * class must be implemented. The 4. *** image class can exist non-* * * Image method, can also exist the method of * * *. The 5. *** Elephant class can not exist * * * Image method. The 5. *** image class is not able to create objects. Question: Why can't the Elephant class create an object? Because the * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * The 6. *** image class is a constructor, and its constructor is provided to the subclass to initialize the properties of the parent class when the object is created. *///Animal Class--->*** Elephant class abstract class animal{string name; String color;public animal (String name,string color) {this.name = Name;this.color = color;} Non-* * * method Public void eat () {System.out.println (name+ "eat Food");} Move ... public abstract void run ();} Dog is a kind of animal class Dog extends Animal{public dog (String name,string color) {super (name,color);} Public void run () {System.out.println (name+ "Four legs run fast ...");} Fish is a kind of animal class fish extends animal{public fish (String name,string color) {super (name,color);} Public void run () {System.out.println (name+) "Shake your tail and swim!" ");}} Class demo3 {public static void main (String[] args) {/*Dog d = new dog ("Shepherd", "Brown");d. Run ();//Create a Fish object Fish f = new fish ("Koi", "golden"); F.run ( ); */animal a = new animal ();}}
/* Requirements: describes a graph, circle, rectangle of three classes. No matter which graph will have the calculation area and perimeter behavior, but each kind of graph calculation way is inconsistent. Naming conventions for constants: Capitalize all letters, separating words from words with underscores. Abstract cannot co-decorate a method with the following keywords, because someone else is rewriting a method that 1. abstract cannot co-decorate with private. 2. abstract cannot work with static to modify a method. 3. abstract cannot work with final to modify a method. *///abstract *** Image//Graphics class Abstract class myshape{ string name;public myshape (String name) {this.name = name;} Public abstract void getarea ();p ublic abstract void getlength ();} Circular is a kind of class circle extends myshape{double r;public static final that belongs to graphic class double pi = 3.14;public circle (String name,double r) {super (name); THIS.R =r;} The area of Public void getarea () {System.out.println (name+) is: "+pi*r*r);} The perimeter of Public void getlength () {System.out.println (name+) is: ' +2*pi*r ';}} Rectangle belongs to the one class rect extends myshape{int width;int height;public in the graph Rect (StrIng name,int width, int height) {super (name);this.width = width;this.height = height;} The area of Public void getarea () {System.out.println (name+) is: "+width*height);} The perimeter of Public void getlength () {System.out.println (name+) is: "+2* (Width+height));}} Class demo4 {public static void main (String[] args) {//System.out.println (" Hello world! "); Circle c = new circle ("Round", 4.0); C.getarea (); C.getlength ();//Rectangle Rect r = new rect ("Rectangle", 3,4); R.getarea (); R.getlength ();}}
This article from "Small Fish Blog" blog, declined reprint!
Java Abstract class