关于java继承性的一个实例


package 继承;

import java.util.Scanner;

/*
 *  定义一个人类,包括属性:姓名;包括方法:吃饭、工作。
 *	(1)根据人类,派生一个学生类,增加属性:学校、学号;重写工作方法(学生的工作是学习)。
 *	(2)根据人类,派生一个教师类,增加属性:单位、工龄;重写工作方法(教师的工作是教学)。
 * 	注意:在每个类中最好都应该有自己的属性的输入输出方法。
 * 
 */
class person {

	String name;
	
	public void input_info(){
		
		System.out.println("input person inforation:");
		System.out.println(" name ");
		Scanner sc = new Scanner(System.in);
		name = sc.next();
		System.out.println();
	
	}
	public void output_info(){
		
		System.out.println("person inforation:");

		System.out.println("person name:  "+name);
		System.out.println();
		
	}
	
	public void eat(){
		
		System.out.println("person like eat...");
		System.out.println();
	}
	
	public void work(){
		
		System.out.println( " person  " + name + " like work...");
		System.out.println();
	}
	
}

class student extends person{
	
	String number;
	String school; 
	
	public void input_info(){
		
		System.out.println("input student inforation:");
		System.out.println("name  number  school");
		
		Scanner sc = new Scanner(System.in);
		name = sc.next();
		number = sc.next();
		school = sc.next();
		System.out.println();
		
	}
	
	public void output_info(){
		
		System.out.println("student inforation:");
		System.out.println("student name:  "+name);
		System.out.println("student number:  " + number);
		System.out.println("student school:  " + school);
		System.out.println();
	}
	
	public void work(){
		
		System.out.println("student  " + name + " like study...");
		System.out.println();
	}
	
}

class techer extends person{

	String workplace;
	int age;
	
	public void input_info(){
		
		System.out.println("input techer inforation:");
		System.out.println("name  workplace  age");
		Scanner sc = new Scanner(System.in);
		name = sc.next();
		workplace = sc.next();
		age = sc.nextInt();
		System.out.println();
		
	}
	
	public void output_info(){
		
		System.out.println("techer inforation:");
		System.out.println("techer name:   "+name);
		System.out.println("techer workplace:   " + workplace);
		System.out.println("techer school:   " + age);
		System.out.println();
		
	}
	
	
    public void work(){
		
		System.out.println("techer  " + name + " like teching...");
		System.out.println();
	}

}









测试类:


package 继承;

public class person_test {

	public static void main(String[] args) {
		
		person p = new person();
		student s = new student();
		techer t = new techer();
		
		p.input_info();
		p.output_info();
		p.work();
		p.eat();
		
		
		s.input_info();
		s.eat();
		s.work();
		s.output_info();
		
		t.input_info();
		t.eat();
		t.work();
		t.output_info();
		
	}

}




测试结果:














  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值