Java 简单java类继承小🌰

[java]
package com.jasonote.classStudy;

import com.sun.deploy.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Jasonote on 2017/11/26.
* a,类的继承的优点
* 子类可以直接将父类的操作继续使用,属于代码重用
* 子类可以继续扩充属于自己的标准
* <p>
* b,类的限制
* 不允许多重继承
* <p>
* c,覆写原则
* 发现父类中的方法功能不足本类的要求,但必须使用这个方法时
* 被覆写的方法不能够拥有比父类更为严格的访问权限(public, default, private)
* 父类的方法被子类覆写,子类的权限只能比父类的方法大
*
* 重载与覆写的区别(Overloading 与 Override)
* 发生范围:一个类里;继承中
* 定义:方法名称参数类型及个数相同;方法名称,参数类型,个数,返回类型相同
* 权限:没有权限限制; 被覆写的方法不能拥有比父类更为严格的访问权限
*
* 在使用重载(Overloading)的时候返回值可以不同,但是考虑到程序设计的统一性,尽量保证一致
*/
public class ExtendsClass {
public static void main(String[] args) {
System.out.println("=================Teach====================");
List<String> classes = new ArrayList<>();
classes.add("大一101班");
classes.add("大一201班");
classes.add("大二301班");
Teach teach = new Teach("Jason", 26, classes, "JAVA语言");
System.out.println(teach);
teach.demo(teach.getName(), teach.getAge());

System.out.println("================Stu=====================");
Stu stu = new Stu("Jasonote", 18, "计算机编程", 107, teach);
System.out.println(stu);
stu.demo(stu.getName(), stu.getAge());

}
}

class Person {

private String name;

private Integer age;

public Person(String name, Integer age) {
this.name = name;
this.age = age;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

void demo(String name, Integer age) {
System.out.println("name:" + name + ";age:" + age);
}

}

class Stu extends Person {
private String StuClass;

private Integer sno;

private Teach teach;

public Stu(String name, Integer age, String stuClass, Integer sno, Teach teach) {
super(name, age);
this.StuClass = stuClass;
this.sno = sno;
this.teach = teach;
}

public String getStuClass() {
return StuClass;
}

public void setStuClass(String stuClass) {
StuClass = stuClass;
}

public Integer getSno() {
return sno;
}

public void setSno(Integer sno) {
this.sno = sno;
}

public Teach getTeach() {
return teach;
}

public void setTeach(Teach teach) {
this.teach = teach;
}

/**
* 覆写demo
*
* @param name
* @param age
*/
@Override
public void demo(String name, Integer age) {
System.out.println("名字:" + name + ";年龄:" + (age – 1));
}

@Override
public String toString() {
return "我的名字是:" + this.getName() + ";\n今年:" + this.getAge() + ";\n我的班级:" + this.getStuClass() + ";\n我的学号:" + this.getSno() + ";\n我的" + teach.getCourse() + "的老师:" + teach.getName();
}
}

class Teach extends Person {
private List<String> classes;

private String course;

public Teach(String name, Integer age, List<String> classes, String course) {
super(name, age);
this.classes = classes;
this.course = course;
}

public List<String> getClasses() {
return classes;
}

public void setClasses(List<String> classes) {
this.classes = classes;
}

public String getCourse() {
return course;
}

public void setCourse(String course) {
this.course = course;
}

/**
* 覆写demo
*
* @param name
* @param age
*/
@Override
public void demo(String name, Integer age) {
System.out.println("—我想调用父类方法啦啦啦啦啦—");
super.demo(name, age);
System.out.println("—-我再补充一些些—-");
}

@Override
public String toString() {
return "我的名字是:" + this.getName() + ";\n今年:" + this.getAge() + ";\n我教的班级有:" + StringUtils.join(this.getClasses(), "; ") + ";\n我教的课程:" + this.getCourse();
}
}

[/java]

Jason.wang

When you find your talent can't afford to be ambitious, be quiet and study !

You may also like...