当前位置: 首页>后端>正文

Java重载和重写

Java重载和重写,第1张

Java多态重载

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重载和重写,第2张

Java多态重写

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();

}

}


https://www.xamrdz.com/backend/3rz1936594.html

相关文章: