JAVA OBJECT 类

Object 接收引用类型数据:

package org.objectdemo;

class Person extends Object{
}

public class ObjectDemo01 {
	public static void main(String args[]){
		int[] a= new int[]{1,2,3,4,5};
		Object o1 = new Person();
		Object o2 = "Hello";
		Object o3 = a;
	}
}

Object 类 toString 方法:

package org.objectdemo;
class Person1{
	public String toString(){
		return "Hello";
	}
}

public class ObjectDemo02 {
	public static void main(String args[]){
		System.out.println(new Person1().toString());
	}
}

Object 类 equales方法:

package org.objectdemo;

class Person2{
	private String name;
	private int age;
	public Person2(String n,int a){
		this.name = n;
		this.age = a;
	}
	public boolean equals(Object p){
		if (this == p){
			return true;
		}
		if (!(p instanceof Person2)){
			return false;
		}
		Person2 per = (Person2)p;
		if ((this.name.equals(per.name)) && (this.age == per.age)){
			return true;
		}
		return false;
	}
}

public class ObjectDemo03 {
	public static void main(String args[]){
		System.out.println(new Person2("张三",20).equals("Hello"));
	}

}


示例:

package org.objectdemo;

abstract class A{
	public A(){
		this.print();
	}
	public abstract void print();
}

class B extends A{
	private int x=100;
	public B(int x){
		this.x = x;
	}
	public void print(){
		System.out.println(x);
	}
}

public class ObjectDemo04 {
	public static void main(){
		A a = new B(10);
		a.print();
	}
}


你可能感兴趣的:(object)