java中的重写override或overwrite_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java中的重写override或overwrite

java中的重写override或overwrite

 2017/6/25 5:32:35  i拾贝  程序员俱乐部  我要评论(0)
  • 摘要:java中的重写override或overwriteTestOverWrite.javaclassPerson{privateStringname;privateintage;publicvoidsetName(Stringname){this.name=name;}publicvoidsetAge(intage){this.age=age;}publicStringgetName(){returnname;}publicintgetAge(){returnage;
  • 标签:ide Java

java中的重写override或overwrite

TestOverWrite.java

?

class="java">class Person {
    private String name;
    private int age;
    public void setName(String name){this.name=name;}
    public void setAge(int age) {this.age=age;}
    public String getName(){return name;}
    public int getAge(){return age;}
    public String getInfo() {
          return "Name: "+ name + "\n" +"age: "+ age;
  }
}

class Student extends Person {
    private String school;
    public String getSchool() {return school;}
    public void setSchool(String school)
    {this.school =school;}
    //重写OverWrite父类的getInfo()方法,访问权限和父类一样,不能更严格
    public String getInfo() {
      return  "Name: "+ getName() + "\nage: "+ getAge() 
                    + "\nschool: "+ school;
		}
}

public class TestOverWrite {
public static void main(String arg[]){
        Student student = new Student();
        Person person = new Person();
        person.setName("none");
        person.setAge(1000);
        student.setName("John");    
        student.setAge(18);
        student.setSchool("SCH");
        System.out.println(person.getInfo());//Name: none age: 1000
        System.out.println(student.getInfo());//Name: John age: 18 school: SCH
    }
}

?

?

发表评论
用户名: 匿名