1. Inheritance
Use an example to illustrate inheritance
Class person{private string name;private int age;public string getname () {return name;} Public void setname (String name) {this.name = name;} Public int getage () {return age;} Public void setage (Int age) {this.age = age;} Public void tell () {System.out.println ("Name:" + this.getname () + " " + "Age:" +this.getage ());}} Class student extends person{private int score;public int getscore () { Return score;} Public void setscore (Int score) {this.score = score;} Public void tell () {Super.tell (); //uses the parent method System.out.println ("Score:" +score);}} public class extenddemo {/** * @param args */public static void main (String[] args) {// todo auto-generated method stubstudent std = new Student (Std.setage); Std.setname ("Thystar"); Std.setscore (+); Std.tell ();}}
2 Abstract classes and excuses
The final keyword declares classes, methods, and properties
-
Classes that use Final declaration cannot be inherited
-
Methods that use Final declaration cannot be overridden
-
Use final declared variables to become constants, constants cannot be modified
Abstract class: A class that contains abstract methods
Abstract class classname{
}
Abstract method: A method declared without being implemented, declared with the abstract keyword
Abstract class Quilt class inheritance, to override all methods in an abstract class
Abstract classes cannot be instantiated directly, but are instantiated by their subclasses
Abstract class abs{private int age;public int getage () {return age;} public void Setage (int.) {this.age = age;} public void Tell () {}public abstract void Say ();p ublic abstract void print (); Class Absclass extends Abs{public void Say () {System.out.println (This.getage ());} public void print () {}}public class Absdemo {public static void main (string[] args) {Absclass ab = new Absclass (); Ab.setage ( Ab.say ();}}
Interface:
Interface intername{
}
Interface inter{public static final int age=10;//Common global constant public abstract void tell (); Interface Inter2{public abstract void Say ();} Abstract class Abs1{public abstract void Print ();} Class A extends Abs implements Inter, Inter2{public void Tell () {}public void Say () {System.out.println ("say");} public void print () {System.out.println ("Abs");}} public class InterDemo01 {/** * @param args */public static void main (string[] args) {//TODO auto-generated method Stuba A = new A (); A.tell (); System.out.println (Inter.age); Global constants Call A.say () with the class name; A.print ();}}
Geek College Course: http://www.jikexueyuan.com/path/javaweb/
Java Object-oriented notation (2)