当前位置:网站首页 > Java基础 > 正文

java基础方法重写与多态



Java多态重载

package model2.unit5;

public class Person extends Object {

    @Override

    public String toString() {

        return "Person{" +

                "name='" + name + ''' +

                ", age=" + age +

                '}';

    }

    public static void main(String[] args) {

        Person person = new Person("张三", 30);

        Person person2 = new Person("张三", 30);

        // Object类中toString方法默认实现:包.类@hash

        System.out.println(person.toString());

        System.out.println(person2.toString());

    }

    String name;

    int age;

    // 多态:重载。

    public Person() {

    }

    public Person(String name) {

        this.name = name;

    }

    public Person(String name, int age) {

        this.name = name;

        this.age = age;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public int getAge() {

        return age;

    }

    public void setAge(int age) {

        this.age = age;

    }

}

Java多态重写

package model2.unit5;

public class Car {

    void m(){

        System.out.println("父类的方法");

    }

}

class Audi extends Car {

    void m(){

        System.out.println("子类的方法");

    }

    public static void main(String[] args) {

        Audi a = new Audi();

        a.m();

java基础方法重写与多态

    }

}

版权声明


相关文章:

  • 0元入学基础学java2024-11-17 21:18:04
  • 疯狂软件Java基础强化营授课视频2024-11-17 21:18:04
  • java基础301集合排序2024-11-17 21:18:04
  • java基础知识学完后制作网站2024-11-17 21:18:04
  • 传智风清扬java基础2024-11-17 21:18:04
  • java及数据清洗基础编程2024-11-17 21:18:04
  • java基础论文前言2024-11-17 21:18:04
  • java基础需要记的2024-11-17 21:18:04
  • java基础实训题目2024-11-17 21:18:04
  • java基础实训日记2024-11-17 21:18:04