1. Abstract class
Package com.yfs.javase;//Letter Template Public abstract class Templater {public abstract string ToName ();p ublic abstract String Fromn Ame ();p ublic abstract String getcontent ();p ublic Void Show () {System.out.println ("respectable" + toname ()); System.out.println ("\ t Hello! "); System.out.println ("\t\t" + getcontent ()); System.out.println ("\t\t\t sincerely \n\t\t\t\t salute"); System.out.println ("\t\t\t\t" + fromname ()); System.out.println ("\t\t\t\t 2015.7.25");}}
2. Inheriting abstract classes
Package Com.yfs.javase;public class Myletter extends Templater {@Overridepublic String toname () {return "Shadow";} @Overridepublic String FromName () {return "YFS";} @Overridepublic String getcontent () {return "to learn Java,so easy!";}} Class Le extends Templater {@Overridepublic String toname () {//TODO auto-generated method Stubreturn "Tom";} @Overridepublic String FromName () {//TODO auto-generated method Stubreturn "Lily";} @Overridepublic String getcontent () {//TODO auto-generated method Stubreturn "Use abstract class";}}
3. Test template
Package Com.yfs.javase;public class Test {public static void main (string[] args) {myletter letter = new Myletter (); Show (); Templater L = new myletter (); L.show (); l = new Le (); L.show ();}}
4. Inheriting instances of classes
Package Com.yfs.javase;public class Student {private String name;private char sex;private int age;private int sno;private Double Middle;private double temp;//to calculate the synthetic result method public double gettotal () {return 0;} @Overridepublic String toString () {return "Student [name=" + name + ", sex=" + Sex + ", age=" + age+ ", sno=" + Sno + ", M Iddle= "+ Middle +", temp= "+ temp+"] ";} Public Student (String name, char sex, int age, int sno, double middle,double temp) {super (); this.name = Name;this.sex = SE X;this.age = Age;this.sno = Sno;this.middle = Middle;this.temp = temp;} Public double Getmiddle () {return middle;} public void Setmiddle (double middle) {This.middle = middle;} Public double gettemp () {return temp;} public void settemp (double temp) {this.temp = temp;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Char Getsex () {return sex;} public void Setsex (char sex) {this.sex = sex;} public int getage () {return age;} public void Setage (int.) {this.age = age;}public int Getsno () {return sno;} public void Setsno (int sno) {This.sno = sno;}}
5. Computer Inheritance Student
Package Com.yfs.javase;public class Computer extends Student {private double operator;private double write;public double g Ettotal () {return operator * 0.4 + write * 0.2 + getmiddle () * 0.2 + gettemp () * 0.2;} Public computer (String name, char sex, int. int, int sno, double middle,double temp, double operator, double write) {super ( Name, sex, age, SNO, middle, temp); this.operator = Operator;this.write = Write;} Public double Getoperator () {return operator;} public void Setoperator (double operator) {this.operator = operator;} Public double Getwrite () {return write;} public void Setwrite (double write) {this.write = write;}}
6.English Inheritance Student
Package Com.yfs.javase;public class 中文版 extends Student {private double speak;public 中文版 (String name, char sex, in T age, Int. SNO, double middle,double temp, double speak) {super (name, sex, age, SNO, middle, temp); this.speak = Speak;} Public double Getspeak () {return speak;} public void Setspeak (double speak) {this.speak = speak;} @Overridepublic Double Gettotal () {return speak * 0.5 + getmiddle () * 0.25 + gettemp () * 0.25;}}
7. Test inheritance
Package Com.yfs.javase;import Java.util.random;public class Studenttest {/** * @param args */public static void main (Strin G[] (args) {student[] s = new Student[5];//s[0] = new 中文版 ("Zhang San", ' Male ', ' 5927 ', ' + ');//system.out.println ("Engli SH: "+ s[0].gettotal ());//s[1] = new computer (" John Doe ", ' female ', +, +, +, +,--);//system.out.println (S[1].gettotal ()) ; String[] names = {"Zhang", "Wang", "Li", "Zhao"}; Random ran = new random (), for (int i = 0; i < s.length; i++) {int r = Ran.nextint (101), if (R >) {s[i] = new Englis H (Names[ran.nextint (4)] + I, Ran.nextboolean ()? ' Male ': ' Female ', 20, 5927, 85, 95, 100);} else {S[i] = new Computer (Names[ran.nextint (4)] + I, Ran.nextboolean ()? ' Male ': ' Female ', 21, 110, 90, 100, 85, 92);}} for (int i = 0; i < s.length; i++) {System.out.println (S[i] + " Comprehensive score:" + s[i].gettotal ());}}}
Java Novice Note 20 abstract class template (letter)