Android 之路15---Java基础9

导读

0.Object类
1.继承自Object的equals方法
2.重写Object中的equals方法
3.继承自Object的toString方法
4.重写Object中的toString方法
5.final关键字
6.注解

Object类

protected Object	clone​()
//克隆一份
boolean	equals​(Object obj)
//String类中实际上重写了此方法
protected void	finalize​()
//在对象回收之前,由垃圾回收器调用的
Class<?>	getClass​()
//java处处皆对象思想的体现,就是返回一个类,在底层类也被当作一个对象处理
int	hashCode​()
//返回该对象的哈希码 ,即对象的逻辑地址(物理地址经过映射得到的)
void	notify​()
//唤醒此对象监视器等待的单个线程
void	notifyAll​()
//唤醒此对象监视器等待的所有线程


继承自Object的equals方法

package com.hala.test;

import com.hala.animal.Dog;

public class Test {

	public static void main(String[] args) {
		
		Dog one=new Dog("二哈",2);
		Dog two=new Dog("二哈",2);
		boolean flag1=one.equals(two);
		//equals测试:继承Object中的equals方法时,比较的是两个引用是否指向同一个对象
		System.out.println("equals值比较结果:"+flag1);
		System.out.println("one 和 two的引用比较结果:"+(one==two));
		String str1=new String("hello");
		String str2=new String("hello");
		boolean flag2=str1.equals(str2);
		//equals在运用到字符串时,实际上是被重写了,它比较的不是引用,而是两个字符串的值是否相同
		//所以flag2的值为true,而==连接的始终比较的是引用值
		System.out.println("equals值比较结果:"+flag2);
		System.out.println("str1 和 str2的引用比较结果:"+(str1==str2));
		
		
	
	}

}


输出结果
equals值比较结果:false
one 和 two的引用比较结果:false
equals值比较结果:true
str1 和 str2的引用比较结果:false

重写Object中的equals方法

Animal父类

package com.hala.animal;

public class Animal {
	
	protected String name;
	private int math;
	private String species;
	
	private static int st1=1;
	public static int st2=2;
	
	
	//父类构造方法不能被继承,不能被重写
	public Animal(){
	}
	
	public Animal(String name,int math){
		this.setName(name);
		this.setMath(math);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getMath() {
		return math;
	}

	public void setMath(int math) {
		this.math = math;
	}

	public String getSpecies() {
		return species;
	}

	public void setSpecies(String species) {
		this.species = species;
	}
	
	//吃东西
	public void eat(){
		System.out.println(this.getName()+"正在吃东西。");
	}

	//这是对Object方法的重写
	public boolean equals(Object obj){
		if(obj==null)
			//若没有此句,会出现空指针异常
			return false;
		Animal temp=(Animal)obj;
		//若没有类型转换,下句会报错
		if(this.getName().equals(temp.getName())&&(this.getMath()==temp.getMath()))
			return true;
		else return false;
	}
	
	//这是对上边方法的重载
	public boolean equals(Animal ani){
		if(ani==null)
			return false;
		if(this.getName().equals(ani.getName())&&(this.getMath()==ani.getMath()))
			return true;
		else return false;
	}
}

package com.hala.test;

import com.hala.animal.Animal;
import com.hala.animal.Dog;

public class Test {

	public static void main(String[] args) {
		
		Animal one=new Animal("二哈",2);
		Animal two=new Animal("二哈",2);
		boolean flag1=one.equals(two);
		//equals测试:重写equals后,可以直接运用
		System.out.println("equals值比较结果:"+flag1);
		System.out.println("one 和 two的引用比较结果:"+(one==two));
		String str1=new String("hello");
		String str2=new String("hello");
		boolean flag2=str1.equals(str2);
		//equals在运用到字符串时,实际上是被重写了,它比较的不是引用,而是两个字符串的值是否相同
		//所以flag2的值为true,而==连接的始终比较的是引用值
		System.out.println("equals值比较结果:"+flag2);
		System.out.println("str1 和 str2的引用比较结果:"+(str1==str2));
		
		
	
	}

}


输出结果

equals值比较结果:true
one 和 two的引用比较结果:false
equals值比较结果:true
str1 和 str2的引用比较结果:false

继承自Object的toString方法

package com.hala.test;

import com.hala.animal.Animal;

public class Test {

	public static void main(String[] args) {
		
		Animal one =new Animal();
		String str1="hello";
		System.out.println(one.toString());
		System.out.println(one);
		/*
		 * toString 测试:
		 * 1.输出对象名时默认会直接调用类中的toString,如两个输出结果
		 * 2.继承自Object中的toString时,输出字符串的形式为:类型信息(工程,包,类名)+@+地址信息。如本例所示
		 */
		
		System.out.println(str1.toString());
		System.out.println(str1);
		//String类型调用toString时,实际上是对Object的toString进行了重写
		
		
		
	
	}

}


输出结果

com.hala.animal.Animal@7852e922
com.hala.animal.Animal@7852e922
hello
hello

在这里插入图片描述

在这里插入图片描述
重写Object中的toString方法

package com.hala.animal;

public class Animal {
	
	protected String name;
	private int math;
	private String species;
	
	private static int st1=1;
	public static int st2=2;
	
	
	//父类构造方法不能被继承,不能被重写
	public Animal(){
	}
	
	public Animal(String name,int math){
		this.setName(name);
		this.setMath(math);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getMath() {
		return math;
	}

	public void setMath(int math) {
		this.math = math;
	}

	public String getSpecies() {
		return species;
	}

	public void setSpecies(String species) {
		this.species = species;
	}
	
	//吃东西
	public void eat(){
		System.out.println(this.getName()+"正在吃东西。");
	}

	//对Object中toString进行重写
	public String toString(){
		return ("名字为:"+this.getName()+"月份为:"+this.getMath());
	}
	
	
}

package com.hala.test;

import com.hala.animal.Animal;

public class Test {

	public static void main(String[] args) {
		
		Animal one =new Animal("花花",2);
		String str1="hello";
		System.out.println(one.toString());
		System.out.println(one);
		
		
		
		
	
	}

}

输出结果

名字为:花花月份为:2
名字为:花花月份为:2

final关键字

/*
 * 1.final class:该类不能被继承,没有子类 public final class/final public class
 * 2.final 方法:该方法可以被继承使用,但不能被重写
 * 3.final 方法内局部变量:只要在使用前赋值即可,一旦赋值不允许修改
 * 4.final 类中的属性成员:赋值方法(1)定义时直接初始化
 * 							  (2)在构造方法中初始化
 * 							  (3)在构造代码块中初始化
 */

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
注解

⚠️调取父类可重写的快捷键:alt /

	//这里是注解,表示重写父类的方法
	@Override
	public void eat() {
		// 继承父类eat方法的格式,要记住!
		super.eat();
		}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值