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基础方法重写与多态
}
}
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/h6javajc/18337.html