• java新手笔记13 继承


    1、Person类

    package com.yfs.javase;
    
    //可以有多个子类
    public class Person {
    
    	private String name;// 私有属性不能继承
    	private int age;
    	private char sex;
    
    	private void privateMehtod() {// 私有方法 类中可以访问
    		System.out.println("call privateMehtod()...");
    	}
    
    	public Person() {
    		// privateMehtod();
    		System.out.println("创建Perosn对象...");
    	}
    
    	public Person(String name) {
    		this.name = name;
    	}
    
    	public Person(String name, int age, char sex) {
    		this.name = name;
    		this.age = age;
    		this.sex = sex;
    	}
    
    	public void introduce() {
    		System.out.println("I am Person....");
    	}
    
    	public String toString() {
    		return "姓名:" + name + "  年龄 :" + age + "  性别:" + sex;
    	}
    
    	public void speak() {
    		System.out.println(name + " 工作了吗?");
    		// privateMehtod();
    	}
    
    	public void sleep() {
    		System.out.println(name + " 睡觉了吗?");
    	}
    
    	public void eat() {
    		System.out.println(name + " 吃了吗?");
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	public char getSex() {
    		return sex;
    	}
    
    	public void setSex(char sex) {
    		this.sex = sex;
    	}
    
    }
    

     2.Student类

    package com.yfs.javase;
    
    //实现代码重用  单继承
    public class Student extends Person {// 继承Person
    
    	private int score;
    
    	@Override
    	public void introduce() {
    		System.out.println("I am student...");
    	}
    	// 覆盖 重写父类的方法
    	@Override
    	public String toString() {
    		return super.toString() + "  成绩:" + score;
    	}
    
    	public int getScore() {
    		return score;
    	}
    
    	public void setScore(int score) {
    		this.score = score;
    	}
    
    	public Student() {// 系统自动调用父类构造方法
    		// super();//调用父类无参数构造方法super()  必须第一句
    		// super("Jack");
    		System.out.println("创建Student对象...");
    	}
    
    	// 扩展方法
    	public void study() {
    		// getName()继承方法
    		// privateMethod();私有方法不能继承
    		// super.toString();//调用父类方法
    		// this super
    		System.out.println(getName() + " 在学习...");// 私有属性能否继承
    	}
    
    }
    
    // 子类下还可以继承
    class YfsStudent extends Student {
    
    }
    

     3.Teacher类

    package com.yfs.javase;
    
    public class Teacher extends Person {
    
    	@Override
    	public void introduce(/*int a*/) {
    		System.out.println("I am teacher...");
    	}
    }
    

    4.Person测试

    package com.yfs.javase;
    
    public class PersonTest {
    
    	public static void main(String[] args) {
    		Person p1 = new Person();
    		p1.setName("张三");
    		p1.setAge(20);
    		p1.setSex('男');
    		System.out.println(p1);//默认toString方法
    		
    		Student s1 = new Student();
    		s1.setName("李四");
    		s1.setAge(22);
    		s1.setSex('女');
    		System.out.println(s1);
    		
    		Teacher t1 = new Teacher();
    		t1.setName("王五");
    		t1.setAge(30);
    		t1.setSex('男');
    		System.out.println(t1);
    		
    		YfsStudent ys = new YfsStudent();
    		ys.setName("Tom");
    		System.out.println(ys);
    		//ys.
    		
    		s1.study();
    		//p1.study();//父类不能调用子类方法
    		ys.study();
    
    	}
    
    }
    

     5.Student测试

    package com.yfs.javase;
    
    public class StudentTest {
    
    	public static void main(String[] args) {
    		//构造方法不能继承
    		//Student s1 = new Student("zhangsan",20,'男');
    		
    		//Student s2 = new Student("lisi");
    		
    		Student s3 = new Student();//系统提供构造方法
    		s3.setName("李四");
    		s3.speak();
    		System.out.println(s3);
    		s3.eat();
    		
    		Teacher t1 = new Teacher();
    		t1.setName("张飞");
    		t1.eat();
    		System.out.println(t1);
    		//Person p1 = new Person("张三",23,'男');
    
    	}
    
    }
    

     6.Teacher类测试

    package com.yfs.javase;
    
    public class TeacherTest {
    
    	public static void main(String[] args) {
    		Person p1 = new Person();
    		p1.setName("张三");
    		
    		Student s1 = new Student();
    		s1.setName("李四");
    		
    		Teacher t1 = new Teacher();
    		t1.setName("王五");
    		
    		p1.introduce();
    		s1.introduce();
    		t1.introduce();
    
    	}
    
    }
    
  • 相关阅读:
    Change the default MySQL data directory with SELinux enabled
    CentOS7使用firewalld打开关闭防火墙与端口
    常用screen参数
    Android手机上浏览器不支持带端口号wss解决方案
    How to Create Triggers in MySQL
    QT GUI @创建新的工程
    Linux内核源代码的结构(转)
    ARM体系的7种工作模式
    C语言中强制数据类型转换(转)
    Linux驱动设计—— 中断与时钟@request_irq参数详解
  • 原文地址:https://www.cnblogs.com/feilongblog/p/4675378.html
Copyright © 2020-2023  润新知