java新手笔记20 抽象类模板(letter)

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

java新手笔记20 抽象类模板(letter)的相关文章

java新手笔记19 抽象类

1.Shap package com.yfs.javase; public class Shape /*extends Object */{ //默认继承object object方法全部继承 //计算面积方法 public double getArea() {//代码完全没用 System.out.println("计算面积"); return 0; } } 2.Circle package com.yfs.javase; public class Circle extends Sh

java新手笔记10 构造器

1.摇奖小程序 package com.yfs.javase; import java.io.IOException; import java.nio.CharBuffer; import java.util.Random; public class Demo1 { /** * 模拟摇奖 */ public static void main(String[] args) { Random ran = new Random(); int[] a = new int[7]; System.out.p

java新手笔记2

1.注释 /** doc注释 * 类说明信息 */ //声明类 文件名与类名一致 public class World {//类定界符 //声明方法 main方法 public static void main(String[] args) { System.out.println("World World!"); //System.out.println("welcome java world!"); //注释的代码不执行 单行注释 /* 多行注释 System.

java新手笔记23 异常

1.import package com.yfs.javase; import java.util.Scanner; //import java.lang.String;//默认导入 public class Demo1 { public static void main(String[] args) { String s = new String("abc");//java.lang.String String s1 = "abc"; System.out.pri

java新手笔记18 类比较

1.Shap类 package com.yfs.javase; public class Shape /*extends Object */{ //默认继承object object方法全部继承 //计算面积方法 public double getArea() { System.out.println("计算面积"); return 0; } } 2.Circle类 package com.yfs.javase; public class Circle extends Shape {

java新手笔记17 参数

package com.yfs.javase; public class ParamDemo { public static void main(String[] args) { int a = 3, b = 5; change(a,b); System.out.println("a = " + a + " b = " + b); Point p = new Point(); p.x = 10; p.y = 20; change(p); System.out.pri

java新手笔记5

1.进制转换 /* 企业发放的奖金根据利润提成. 利润(I)低于或等于10万元时,奖金可提10%: 利润高于10万元,低于20万元时, 低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%: 20万到40万之间时,高于20万元的部分,可提成5%: 40万到60万之间时高于40万元的部分,可提成3%: 60万到100万之间时,高于60万元的部分,可提成1.5%, 高于100万元时,超过100万元的部分按1%提成, 从键盘输入当月利润I,求应发放奖金总数? */ import java

java新手笔记4

1.数组 import java.util.Random; public class ArrayDemo1 { public static void main(String[] args) { int a = 10; int b = 20;// //数组对象 创建10个变量 arr[0].arr[1].arr[2].arr[3]....arr[9] int[] arr = new int[10];//默认0 arr[0] = 35; arr[1] = 29; System.out.println

java新手笔记22 接口示例2

1.USB package com.yfs.javase; public interface USB { //定义规范 public void read(); public void write(); } 2.UDisk读写 package com.yfs.javase; public class UDisk implements USB { @Override public void read() { System.out.println("从U盘读取数据..."); } @Over