要领的重写:
1、在子类中能够根据需要对从基类中继续来的要领举行重写。
2、重写的要领和被重写的要领必需具有雷同要领称号、参数列表和返回范例。
3、重写要领不能运用比被重写的要领更严厉的接见权限。
在线视频教程分享:java在线视频
示例以下:
class Person{ private int age; private String name; public void setAge(int age){ this.age = age; } public void setName(String name){ this.name = name; } public int getAge(){ return age; } public String getName(){ return name; } public String getInfo(){ return "Name is:"+name+",Age is "+age; } } class Student extends Person{ private String school; public void setSchool(String school){ this.school = school; } public String getSchool(){ return school; } public String getInfo(){ return "Name is:"+getName()+",Age is "+getAge()+",School is:"+school; } } public class TestOverRide{ public static void main (String args[]){ Student student = new Student(); Person person = new Person(); person.setAge(1000); person.setName("lili"); student.setAge(23); student.setName("vic"); student.setSchool("shnu"); System.out.println(person.getInfo()); System.out.println(student.getInfo()); } }
实行效果:
相干文章教程引荐:java言语入门
以上就是java中怎样重写一个要领的细致内容,更多请关注ki4网别的相干文章!