1.抽象类
package com.yfs.javase; //信模板 public abstract class Templater { public abstract String toName() ; public abstract String fromName(); public abstract String getContent(); public void show() { System.out.println("尊敬的" + toName()); System.out.println("\t您好!"); System.out.println("\t\t" + getContent()); System.out.println("\t\t\t 此致 \n\t\t\t\t 敬礼" ); System.out.println("\t\t\t\t" + fromName()); System.out.println("\t\t\t\t 2015.7.25" ); } }
2.继承 抽象类
package com.yfs.javase; public class MyLetter extends Templater { @Override public String toName() { return "影分身"; } @Override public String fromName() { return "yfs"; } @Override public String getContent() { return "来学java,so easy!"; } } class le extends Templater { @Override public String toName() { // TODO Auto-generated method stub return "Tom"; } @Override public String fromName() { // TODO Auto-generated method stub return "lily"; } @Override public String getContent() { // TODO Auto-generated method stub return "使用抽象类"; } }
3.测试 模板
package com.yfs.javase; public class Test { public static void main(String[] args) { MyLetter letter = new MyLetter(); letter.show(); Templater l = new MyLetter(); l.show(); l = new le(); l.show(); } }
4.类的继承 实例
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; // 计算综合成绩方法 public double getTotal() { return 0; } @Override public String toString() { return "Student [name=" + name + ", sex=" + sex + ", age=" + age + ", sno=" + sno + ", middle=" + middle + ", temp=" + temp + "]"; } public Student(String name, char sex, int age, int sno, double middle, double temp) { super(); this.name = name; this.sex = sex; 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 age) { this.age = age; } public int getSno() { return sno; } public void setSno(int sno) { this.sno = sno; } }
5. Computer 继承Student
package com.yfs.javase; public class Computer extends Student { private double operator; private double write; public double getTotal() { return operator * 0.4 + write * 0.2 + getMiddle() * 0.2 + getTemp() * 0.2; } public Computer(String name, char sex, int age, 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 继承 Student
package com.yfs.javase; public class English extends Student { private double speak; public English(String name, char sex, int 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; } @Override public double getTotal() { return speak * 0.5 + getMiddle() * 0.25 + getTemp() * 0.25; } }
7.测试 继承
package com.yfs.javase; import java.util.Random; public class StudentTest { /** * @param args */ public static void main(String[] args) { Student[] s = new Student[5]; // s[0] = new English("张三", ‘男‘, 20, 5927, 85, 95, 100); // System.out.println("english : " + s[0].getTotal()); // s[1] = new Computer("李四", ‘女‘, 21, 110, 90, 100, 85, 92); // System.out.println(s[1].getTotal()); String[] names = {"张","王","李","赵"}; Random ran = new Random(); for (int i = 0; i < s.length; i++) { int r = ran.nextInt(101); if(r > 50) { s[i] = new English(names[ran.nextInt(4)] + i, ran.nextBoolean() ? ‘男‘ : ‘女‘, 20, 5927, 85, 95, 100); } else { s[i] = new Computer(names[ran.nextInt(4)] + i, ran.nextBoolean() ? ‘男‘ : ‘女‘, 21, 110, 90, 100, 85, 92); } } for (int i = 0; i < s.length; i++) { System.out.println(s[i] + " 综合成绩 :"+ s[i].getTotal()); } } }
时间: 2024-10-28 14:09:01