zoukankan      html  css  js  c++  java
  • JAVA 复写

    子类对introduce进行复写

    public class Person {
    
        String name;
        int age;
    
        Person(String name, int age) {
            this.name = name;
            this.age = age;
            System.out.println("Person 二参构造");
        }
    
        void introduce() {
            System.out.println("我的名字是" + name + ", 我的年龄是" + age);
        }
    }
    public class Student extends Person {
    
        String address;
    
        public Student(String name, int age, String address) {
            super(name, age);
            this.address = address;
        }
    
        void introduce() {
            System.out.println("我的名字是" + name + ", 我的年龄是" + age + ", 我的地址是" + address);
        }
    
    }
    public class Test {
    
        public Test() {
            // TODO Auto-generated constructor stub
        }
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Student student = new Student("furong", 12, "BeiJing");
            student.introduce();
        }
    
    }

    运行结果

    Person 二参构造
    我的名字是furong, 我的年龄是12, 我的地址是BeiJing

    子类改进

    public class Student extends Person {
    
        String address;
    
        public Student(String name, int age, String address) {
            super(name, age);
            this.address = address;
        }
    
        void introduce() {
            super.introduce();
            System.out.println("我的地址是" + address);
        }
    
    }
  • 相关阅读:
    To do list
    2020 上半学期比赛记录
    板子
    Project Euler 1~10 野蛮题解
    卡常火车头
    防止unordered_map 被卡方法
    2019 香港区域赛 BDEG 题解
    2019徐州区域赛 ACEFM 题解 & pollard-rho & miller-rabin & 求出每个子树的重心 板子
    BST-splay板子
    ZJOI2017(2) 游记
  • 原文地址:https://www.cnblogs.com/zhangxuechao/p/13595584.html
Copyright © 2011-2022 走看看