久染

导航

Java day5【第十四章】继承案例分析

【第十四章】继承案例分析:

一.案例分析类:

  建立一个人类和学生类,功能要求如下:

 (1)Person中包含四个私有型的数据成员name,addr,sex,age,分别为字符串型,字符串型,字符型和整型。

  一个四参构造,一个俩参构造,一个无参构造,一个输出方法显示四种属性。

   (2)Student继承Person类,并增加成员math,English存放数学和英语成绩。一个六参构造方法,一个俩参构造方法,一个无参构造方法和重写输出方法用于显示六种属性。

class Person
{
	private String name;
	private String addr;
	private char sex;
	private int age;
	public Person(){}
	public Person(String name ,String addr){
		this(name,addr,'男',0);
	}
	public Person(String name, String addr, char sex, int age){
		this.name = name;
		this.addr = addr;
		this.sex = sex;
		this.age = age;
	}
	public String getInfo(){
		return "姓名:"+this.name + "\t地址:"+this.addr+ "\t性别:"+this.sex + "\t年龄:"+this.age;
	}
}

class Student extends Person
{
		private double math;
		private double english;
		public Student(String name ,String addr){
			super(name,addr);
		}
		public Student(String name,String addr,char sex,int age,double math,double english){
			super(name,addr,sex,age);
			this.math = math;
			this.english = english;
		}
		public String getInfo(){
			return super.getInfo()+ "\t数学成绩:"+this.math + "\t英语成绩:"+this.english;
		}
}

public class Tsy
{
	public static void main(String[] args) 
	{
		Student stu = new Student("同思远","白宫",'男',19,150.00,150.00);
		System.out.println(stu.getInfo());

	}
}

  

二.案例分析二:

  定义员工类,具有姓名、年龄、性别属性,并具有构造方法和显示数据方法

  定义管理层类,继承员工类,并有自己的属性,职务和年薪。

  定义职员类,继承员工类,并由自己的属性,所属部门和月薪。

class Employee
{
	private String name;
	private int age;
	private String sex;
	public Employee(){}			//先保证无参,保证子类继承。
	public Employee(String name,int age,String sex) {
		this.name = name;
		this.age = age;
		this.sex = sex;
	}
	public String getInfo(){
		return "姓名:"+this.name +"、年龄:"+this.age +"、性别:"+this.sex;
	}

}

class Manager extends Employee {			//管理层
	private String job;
	private double income;
	public Manager(){}
	public Manager(String name,int age,String sex,String job,double income){
		super(name,age,sex);
		this.job = job;
		this.income = income;
	}
	public String getInfo(){
		return "【管理层】"+super.getInfo()+"、职务:"+this.job + "、年薪:" + this.income;
	}

}

class Staff extends Employee {
	private String dept;
	private double salary;
	public Staff(){}
	public Staff(String name,int age,String sex,String dept,double salary){
		super(name,age,sex);
		this.dept = dept;
		this.salary = salary;
	}
	public String getInfo(){
		return "【职员层】"+super.getInfo()+"、部门:"+this.dept + "、月薪:" + this.salary;
	}

}

 public class tsy
{
	public static void main(String[] args) 
	{
		Manager man = new Manager("同思远",19,"男","总统",1500000000.00);
		Staff staA = new Staff("薛子扬",19,"男","梓同科技有限公司",15000000.00);
		Staff staB = new Staff("同林艺",18,"男","梓同科技有限公司",15000000.00);
		System.out.println(man.getInfo());
		System.out.println(staA.getInfo());
		System.out.println(staB.getInfo());
	}
}

  

三.编写程序,统计出字符串“want you to know one thing”中字母n和o出现的次数

 

posted on 2019-10-17 13:59  久染  阅读(399)  评论(0)    收藏  举报