Java学习笔记Object类,包装类,内部类

我的Java学习笔记

Object类,包装类,内部类



Object类,包装类,内部类

1.Object类

1.1 equals()方法

public class ObjectTest {
	public static void main(String[] args) {
		boolean equals = "abc".equals("abc");
		System.out.println(equals);
	}
}
//equals() 作用:比较两个字符串内容是否相等
public class ObjectTest {
	public static void main(String[] args) {
		boolean equalsIgnoreCase = "abc".equalsIgnoreCase("AbC");
		System.out.println(equalsIgnoreCase);
	}
}	
//equalsIgnoreCase() 比较两个字符串内容是否相同   不区别大小写
1.2 hashCode()方法

public class ObjectTest {
	public static void main(String[] args) {
		User user = new User();
		System.out.println(user.hashCode());
	}
}
//hashCode() 获取对象的哈希值
1.3 toString()方法
public class ObjectTest {
	public static void main(String[] args) {
		User user = new User();
		System.out.println(user.toString());
	}
}
//原始的toString方法就是输出getClass().getName() + "@" + Integer.toHexString(hashCode());
//一般在实际开发中类会重写toString()
@Override
public String toString() {
		// TODO Auto-generated method stub
	return  this.eid+","+this.ename+","+this.gender+","+this.money+","+this.dept.getDname();
}

2.包装类

2.1什么是包装类?

在实际开发过程中,我们经常会遇到需要使用对象,而不是内置数据类型的情形。为了解决这个问题,Java 语言为每一个内置数据类型提供了对应的包装类。

包装类基本数据类型
Booleanboolean
Bytebyte
Shortshort
Integerint
Longlong
Characterchar
Float float
Doubledouble
2.2包装类的使用
// 基本数据类型转换为包装类
// 调用包装类的构造器

public class Test1 {
    public static void main(String[] args) {
        int num = 10;

        Integer in1 = new Integer(num);
        Integer in2 = new Integer("123");
        //Integer in3 = new Integer("asc")会报异常
        System.out.println(in1.toString());
        System.out.println(in2.toString());

        Float f1 = new Float(1.2f);
        Float f2 = new Float("1.2f");
        System.out.println(f2.toString());

        Boolean b1 = new Boolean(true);
        Boolean b2 = new Boolean("true");
        Boolean b3 = new Boolean("123true");//输出False
        
    }
}
2.3 包装类转换成基本数据类型
public class Test2 {
    public static void main(String[] args) {

        Integer in1 = new Integer(123);
        int i1 = in1.intValue();
        System.out.println(i1);

        Float f1 = new Float(1.23);
        float f2 = f1.floatValue();
        System.out.println(f2);

    }
}
2.4自动装箱和自动拆箱
public class Test3 {
    public static void main(String[] args) {

        //自动装箱:基本数据类型--->包装类
        int i1 = 123;
        Integer in1 = i1;
        System.out.println(in1.toString());

        //自动拆箱:包装类--->基本数据类型
        float f2 = 1.23f;
        Float f1 = f2;
        System.out.println(f1);
    }
}
2.5 包装类转换成String
public class Test4 {
    public static void main(String[] args) {

        // 方式一:采用拼接
        int i1 = 123;
        String s1 = i1 + "";
        System.out.println(s1);

        // 方式二:使用String.valuOf()
        float f1 = 1.23f;
        String s2 = String.valueOf(f1);
        System.out.println(s2);

        Double d1 = new Double(1.22);
        String s3 = String.valueOf(d1);
        System.out.println(s3);

    }
2.6 String转换成包装类
public class Test5 {
    public static void main(String[] args) {

        String s1 = "123";
        int i = Integer.parseInt(s1);
        System.out.println(i);

        String s2 = "true";
        boolean b1 = Boolean.parseBoolean(s2);
        System.out.println(b1);

    }
}

3.内部类

3.1 什么是内部类

​ 内部类:定义在类中类就是内部类

3.2 匿名内部类

​ 定义:定义在类中没有名字的类

​ 1.匿名内部类发生在实例化抽象类

//抽象类
public abstract class Emp {
	
	public abstract void add();
}
//测试类
public class Test {
	public static void main(String[] args) {
        // Emp()后就是匿名内部类
		Emp p = new Emp() {
			@Override
			public void add() {
				System.out.println("这是匿名内部类中add的方法");
			}
		};
		p.add();
	}
}

总结来说:当抽象类中出现了抽象方法,实例化抽象类的时候需要生成匿名内部类

好处:当抽象中的抽象方法明确只使用一次时,使用匿名内部类的方式

​ 当抽象类中的方法有不同形态的时候,使用匿名内部类

3.3 内部类的分类

​ 1.成员内部类(定义在类中的类。和类中的成员变量和方法同级的)

public class User {
	private String name;
	//成员内部类
	class Test {
		void add() {
			System.out.println("add方法");
		}
	}
}
//实例化成员内部类
public class Test {
	public static void main(String[] args) {
		Emp emp = new Emp();
		Demo demo = emp.new Demo();
	}
}

​ 2.局部内部类(定义在方法中类,称之为局部内部类)

public class User {
	public void add() {
		String s;
        //局部内部类和局部变量一样,不能使用public protected private static修饰符
		class Woman{
			
		}
	}
}

​ 3.匿名内部类

​ 4.静态内部类

public class User {
	static class Test{
		public void add() {
			System.out.println("add");
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值