您的位置:首页 > 移动开发 > Objective-C

马士兵J2SE-第三章-面向对象-Object类:toString方法、equals方法、对象转型、抽象类抽象方法、final关键字、接口

2011-11-11 09:42 806 查看
toString方法

public class test {
public static void main(String[] args) {
Teacher t=new Teacher();
System.out.println("1 "+t+" 2 "+t.toString());
}
}

class Teacher {
public String toString() {
return "I am a teacher";
}
}


输出:

1 I am a teacher 2 I am a teacher

说明:

public String toString() {return "I am a teacher";}不能写成大写的public String ToString!

重写必须要COPY过来!

toString

public String toString()

返回该对象的字符串表示。通常,
toString
方法会返回一个“以文本方式表示”此对象的字符串。结果应是一个简明但易于读懂的信息表达式。建议所有子类都重写此方法。
Object
类的
toString
方法返回一个字符串,该字符串由类名(对象是该类的一个实例)、at 标记符“
@
”和此对象哈希码的无符号十六进制表示组成。换句话说,该方法返回一个字符串,它的值等于:

getClass().getName() + '@' + Integer.toHexString(hashCode())


返回: 该对象的字符串表示形式。

认识一下toString方法,并且重写父类方法!

equals方法:

public class test {
public static void main(String[] args) {
Cat c1=new Cat(1,2,3);
Cat c2=new Cat(1,2,3);
System.out.println(c1==c2);
}
}

class Cat {
int color;
int height,weight;
public Cat(int color,int height,int weight) {
this.color=color;
this.height=height;
this.weight=weight;
}
}

//永远不等,比较的是地址


输出:

false

public class test {
public static void main(String[] args) {
Cat c1=new Cat(1,2,3);
Cat c2=new Cat(1,2,3);
System.out.println(c1==c2);
System.out.println (c1.equals(c2));
}
}

class Cat {
int color;
int height,weight;
public Cat(int color,int height,int weight) {
this.color=color;
this.height=height;
this.weight=weight;
}

//重写方法
public boolean equals(Object obj) {
return true;
}//永远都相等,不好!就是写在这里需要注意一下重写要从JDK文档中COPY一下
}

//永远不等,比较的是地址


输出:

false

true

重写equals方法正确写法

public class test {
public static void main(String[] args) {
Cat c1=new Cat(1,2,3);
Cat c2=new Cat(1,2,3);
System.out.println(c1==c2);
System.out.println (c1.equals(c2));
}
}

class Cat {
int color;
int height,weight;

public Cat(int color,int height,int weight) {
this.color=color;
this.height=height;
this.weight=weight;
}

//重写方法
public boolean equals(Object obj) {
if(obj==null) return false;
else {
if(obj instanceof Cat) {//如果obj是猫对象的一个引用
Cat c=(Cat)obj;//obj强制转化为猫类型
if(c.color==this.color&&c.height==this.height&&c.weight==this.weight) {
return true;
}
}
}
return false;
}
}


输出:

false

true

public class test {
public static void main(String[] args) {
Cat c1=new Cat(1,2,3);
Cat c2=new Cat(1,2,3);
System.out.println(c1==c2);
System.out.println (c1.equals(c2));

String s1=new String("Hello");
String s2=new String("Hello");
System.out.println(s1==s2);

//java.lang包下的string重写了equals方法
//将此字符串与指定的对象比较。当且仅当该参数不为 null,并且是与此对象表示相同字符序列的 String 对象时,结果才为 true。
System.out.println(s1.equals(s2));//s2不为空,是true
}
}

class Cat {
int color;
int height,weight;

public Cat(int color,int height,int weight) {
this.color=color;
this.height=height;
this.weight=weight;
}

//重写方法
public boolean equals(Object obj) {
if(obj==null) return false;
else {
if(obj instanceof Cat) {//如果obj是猫对象的一个引用
Cat c=(Cat)obj;//obj强制转化为猫类型
if(c.color==this.color&&c.height==this.height&&c.weight==this.weight) {
return true;
}
}
}
return false;
}
}


输出:

false

true

false

true

equals

public boolean equals(Object obj)

指示其他某个对象是否与此对象“相等”。
equals
方法在非空对象引用上实现相等关系:

自反性:对于任何非空引用值
x
x.equals(x)
都应返回
true

对称性:对于任何非空引用值
x
y
,当且仅当
y.equals(x)
返回
true
时,
x.equals(y)
才应返回
true

传递性:对于任何非空引用值
x
y
z
,如果
x.equals(y)
返回
true
,并且
y.equals(z)
返回
true
,那么
x.equals(z)
应返回
true

一致性:对于任何非空引用值
x
y
,多次调用 x.equals(y) 始终返回
true
或始终返回
false
,前提是对象上
equals
比较中所用的信息没有被修改。
对于任何非空引用值
x
x.equals(null)
都应返回
false


Object
类的 equals 方法实现对象上差别可能性最大的相等关系;即,对于任何非空引用值
x
y
,当且仅当
x
y
引用同一个对象时,此方法才返回
true
x == y
具有值
true
)。

注意:当此方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码。

参数:
obj
- 要与之比较的引用对象。 返回: 如果此对象与 obj 参数相同,则返回
true
;否则返回
false
另请参见:
hashCode()
,
Hashtable


对象转型:

class Animal {
public String name;
Animal(String name) {
this.name=name;
}
}

class Cat extends Animal {
public String eyesColor;
Cat(String n,String c) {
super(n);
eyesColor=c;
}
}

class Dog extends Animal {
public String furcolor;
Dog(String n,String c) {
super(n);
furcolor=c;
}
}

public class test {
public static void main(String[] args) {
Animal a=new Animal("name");
Cat c=new Cat("name","blue");
Dog d=new Dog("dogname","black");

System.out.println(a instanceof Animal);//true
System.out.println(c instanceof Animal);//true
System.out.println(d instanceof Animal);//true
System.out.println(a instanceof Cat);//false

a=new Dog("bigyellow","yellow");;
System.out.println(a.name);//bigyellow
//System.out.println(a.furname);//error!
System.out.println(a instanceof Animal);//true
System.out.println(a instanceof Dog);//true
Dog d1=(Dog)a;//要加强制你转换符
System.out.println(d1.furcolor);//yellow

}
}


输出:

true

true

true

false

bigyellow

true

true

yellow

class Animal {
public String name;
Animal(String name) {
this.name=name;
}
}

class Cat extends Animal {
public String eyesColor;
Cat(String n,String c) {
super(n);
eyesColor=c;
}
}

class Dog extends Animal {
public String furcolor;
Dog(String n,String c) {
super(n);
furcolor=c;
}
}

public class test {
public static void main(String[] args) {
test testdemo=new test();
Animal a=new Animal("name");
Cat c=new Cat("catname","blue");
Dog d=new Dog("dogname","black");
testdemo.f(a);
testdemo.f(c);
testdemo.f(d);
}

public void f(Animal a) {
System.out.println("name:"+a.name);
if(a instanceof Cat) {
Cat cat=(Cat) a;
System.out.println(" "+cat.eyesColor+" eyes");
}
else if(a instanceof Dog) {
Dog dog =(Dog) a;
System.out.println(" "+dog.furcolor+" fur");
}
}
}
输出:

name:name

name:catname

blue eyes

name:dogname

black fur

abstract class Animal {//有了抽象方法,这个类必须被声明为抽象类
private String name;
Animal(String name) {this.name = name;}
/*
public void enjoy(){
System.out.println("叫声......");
}
*/
//抽象类的方法没有写的必要
public abstract void enjoy();//只有;没有左右大括号定义,相当于C++里的纯虚函数
}

abstract class Cat extends Animal {
private String eyesColor;
Cat(String n,String c) {super(n); eyesColor = c;}
/*
public void enjoy() {
System.out.println("猫叫声......");
}
*/
//public abstract void enjoy();
}

class Dog extends Animal {
private String furColor;
Dog(String n,String c) {super(n); furColor = c;}

public void enjoy() {
System.out.println("狗叫声......");
}
}

class Bird extends Animal {
Bird() {
super("bird");
}
public void enjoy() {
System.out.println("鸟叫声......");
}
}

class Lady {
private String name;
private Animal pet;
Lady(String name,Animal pet) {
this.name = name; this.pet = pet;
}
public void myPetEnjoy(){pet.enjoy();}
}

public class test {
public static void main(String args[]){
//Cat c = new Cat("catname","blue");//抽象的类是残缺的类,NEW不出来
Dog d = new Dog("dogname","black");
Bird b = new Bird();
//Lady l1 = new Lady("l1",c);
Lady l2 = new Lady("l2",d);
Lady l3 = new Lady("l3",b);
//l1.myPetEnjoy();
l2.myPetEnjoy();
l3.myPetEnjoy();
}
}


输出:

狗叫声......

鸟叫声......

final关键字

public class test {
public static void main(String[] args) {
T t=new T();
System.out.println(t.i);
}
}

final class T {
final int i = 8;
public final void m() {
//j=9;
}
}


输出:

8

说白了,就是只可以读不可以写

接口:

interface Singer {
public void sing();
public void sleep();
}

interface Painter {
public void paint();
public void eat();
}

class Student implements Singer {
private String name;

Student(String name) {
this.name=name;
}

public void study() {
System.out.println("studying");
}

public String getName() {
return name;
}

public void sing() {
System.out.println("student is singing");
}

public void sleep() {
System.out.println("student is sleeping");
}
}

class Teacher implements Singer,Painter {
private String name;

public String getString() {
return name;
}

Teacher(String name) {this.name=name;}

public void teach() {
System.out.println("teacher is teaching");
}

public void sing() {
System.out.println("teacher is singing");
}
public void sleep() {
System.out.println("teacher is sleeping");
}
public void eat() {
System.out.println("teacher is eating");
}

@Override
public void paint() {
// TODO Auto-generated method stub
System.out.println("teacher is painting");
}
}

public class test {
public static void main(String[] args) {
Singer s1=new Student("tom");
s1.sing();
s1.sleep();
Teacher t1=new Teacher("jack");
t1.eat();
t1.sing();
t1.sleep();
t1.paint();
Painter p1=(Painter)t1;
p1.paint();
p1.eat();
}

}


输出:

student is singing

student is sleeping

teacher is eating

teacher is singing

teacher is sleeping

teacher is painting

teacher is painting

teacher is eating
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐