方法的重写【java语言】

  1、父类

  package com.wyq.study;

  public class Father{//书写类

  //书写属性

  private String name;

  private int age;

  //提供共有的取值赋值方法

  public void setName(String name){

  this.name = name;

  }

  public String getName(){

  return name;

  }

  public void setAge(int age){

  if(age<0||age>100){

  System.out.println("年龄输入有误,请重新输入。");

  this.age = 1;

  }else{

  this.age = age;

  }

  }

  public int getAge(){

  return age;

  }

  //书写无参构造方法

  public Father(){

  System.out.println("这里是Father的无参的构造方法");

  }

  public Father(String name,int age){

  this.name =name;

  this.setAge(age);

  System.out.println("这里是Father的带参的构造方法,参数分别是:"+this.name+"\t"+this.getAge());

  }

  //书写普通方法

  public void speak(String language,String work){

  System.out.println("这里是父类Father普通方法的测试:其参数为:"+language+"\t"+work+"\t"+this.name+"\t"+this.getAge());

  }

  }

  注意父类的speak方法

  2、再写子类

  package com.wyq.study;

  public class Son extends Father{

  //属性

  private String schoolName;

  private int clazz;

  //属性私有化

  public void setSchoolName(String schoolName){

  this.schoolName = schoolName;

  }

  public String getSchoolName(){

  return schoolName;

  }

  public void setClazz(int clazz){

  if(clazz<0||clazz>10){

  System.out.println("班级的输入有误。");

  this.clazz = 5;

  }else

  this.clazz = clazz;

  }

  public int getClazz(){

  return clazz;

  }

  //无参构造

  public Son(){

  super();

  System.out.println("这里是son类的无参构造");

  }

  public Son(String name,int age,String schoolName,int clazz){

  super( name, age);

  this.schoolName = schoolName;

  this.setClazz(clazz);

  System.out.println("这里是son类的带参构造"+"\t"+super.getName()+"\t"+super.getAge()+"\t"+this.getSchoolName()+"\t"+this.schoolName+"\t"+this.getClazz());

  }

  //书写普通方法无锡人流医院:http://www.bhnfkyy.com/

  public void study(String course){

  System.out.println("正在学习"+course+"\t"+this.schoolName+"\t"+this.getClazz());

  }

  @Override

  public void speak(String language, String work) {

  super.speak(language, work);

  System.out.println("这里是重写子类Son的测试,参数为:"+super.getName()+"\t"+super.getAge()+"\t"+this.getSchoolName()+"\t"+this.getClazz());

  }

  }

  3、再写测试类

  package com.wyq.study;

  public class TestSon {

  public static void main(String[] args) {

  Son s = new Son();//调用子类的无参构造

  Son so = new Son("张三",20,"北京大学",2);

  so.study("计算机");

  System.out.println(so.getName()+"\t"+so.getAge()+"\t"+so.getSchoolName()+"\t"+so.getClazz());

  so.speak("python", "码农");

  System.out.println("************************");

  Father f = new Father("王五",-10);

  f.speak("C语言", "工人");

  }

  }

  4、测试结果

  这里是Father的无参的构造方法

  这里是son类的无参构造

  这里是Father的带参的构造方法,参数分别是:张三 20

  这里是son类的带参构造 张三 20 北京大学 北京大学 2

  正在学习计算机 北京大学 2

  张三 20 北京大学 2

  这里是父类Father普通方法的测试:其参数为:python 码农 张三 20

  这里是重写子类Son的测试,参数为:张三 20 北京大学 2

  ************************

  年龄输入有误,请重新输入。

  这里是Father的带参的构造方法,参数分别是:王五 1

  这里是父类Father普通方法的测试:其参数为:C语言 工人 王五 1

  5、总结

  1)继承:先写父类,再写子类;可以继承父类非private属性,可以继承父类非private方法;对于父类的构造方法,子类可以调用,但是不能继承。继承使用的关键字是extends

  2)最左侧有"绿色"的实心三角,说明这个方法是重写父类中的方法,方法上会有@的注解

  3)方法的重写

  ①在子类

  ②方法的名称必须与父类的方法名称相同

  ③子类所继承的方法的必须与父类该方法的参数类型、顺序、个数完全相同

  ④子类重写方法的返回值类型必须与父类方法的返回值类型相同,或者与父类的子类的方法返回值类型相同。

posted on 2019-06-25 15:49  小码农159  阅读(208)  评论(0编辑  收藏  举报