package dynamic;
public class A {
public int i = 10 ;
public int sum()
{
return getI() + 10;
}
public int getI()
{
return i ;
}
public int sum1()
{
return i + 10;
}
}
package dynamic;
public class B extends A{
public int i = 20 ;
public int sum()
{
return getI() + 20;
}
public int getI()
{
return i ;
}
public int sum1()
{
return i + 10;
}
}
package dynamic;
public class main {
public static void main(String[] args) {
A a = new B();
System.out.println(a.sum());
System.out.println(a.sum1());
}
}
package dynamicshuzu;
public class main {
public static void main(String[] args) {
//继承结构 一个Person 两个学生 两个老师 放在一个数组中
Person [] persons = new Person[5];
persons[0] = new Person("jack", 55) ;
persons[1] = new Student("jack", 22,100) ;
persons[2] = new Student("jack", 20,50) ;
persons[3] = new Teacher("jack", 40,2000) ;
persons[4] = new Teacher("jack", 35,3000) ;
//循环调用say方法
for (int i = 0; i < 5; i++) {
System.out.println(persons[i].say()); // 动态绑定机制
}
}
}
package dynamicshuzu;
public class Person {
private String name ;
private int age;
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;
}
public String say()
{
return name + "\t" + age ;
}
}
package dynamicshuzu;
public class Student extends Person{
private double score;
public Student(String name, int age, double score) {
super(name, age);
this.score = score;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String say() {
return super.say() + " score= " + score;
}
}
package dynamicshuzu;
public class Teacher extends Person{
private double salary;
public Teacher(String name, int age, double salary) {
super(name, age);
this.salary = salary;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String say() {
return super.say() + " salary= " + salary;
}
}
package dynamicshuzu;
public class main {
public static void main(String[] args) {
Integer int1 = new Integer(1000);
Integer int2 = new Integer(1000);
System.out.println(int1 == int2);//false new出的两个地址不一样
System.out.println(int1.equals(int2));//true 方法重写 判断的是int值 1000
String s1 = new String("xlx");
String s2 = new String("xlx");
System.out.println(s1 == s2);//false new出的两个地址不一样
System.out.println(s1.equals(s2));//true 方法重写 判断的是string字符串是否一样
}
}
package ex;
public class ex1 {
public static void main(String[] args) {
Person person1 = new Person("jack",10,'m');
Person person2 = new Person("jack",10,'m');
System.out.println(person1.equals(person2)); // 假 因为不是一个对象 继承的object的equals方法
//重写之后就会变成true
}
}
class Person {
private String name;
private int age;
private char gender;
public boolean equals(Object obj)
{
//如果比较的两个对象是同一个对象,则直接返回true
if(this == obj) return true;
//如果传入的是Person类型的话
if (obj instanceof Person) {
Person p = (Person)obj ; // 向下转型
return this.name.equals(p.name)&&this.age==p.age&&this.gender==p.gender;
}
return false;
}
public Person(String name, int age, char gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
}
使用快捷键即可
alt + insert 然后找toString(重写后,一般是把对象的自己的属性输出)
当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法