DAY08

1.对象类型转换:

public class Test 
{
	public static void main(String[] args) 
	{
//		int i = 10;
//		long l = i;
		
//		long l = 101;
//		int i = (int)l;
		
//		从子类到父类的类型转换可以自动进行
//		Student s = new Student();
//		Person p = s;
		
//		父类到子类必须通过强转实现
//		Person p = new Person();
//		Student s = (Student) p;
	}
}

2.equals、==:

在这里插入图片描述
在这里插入图片描述

练习:
在这里插入图片描述

public class Order
{
	public Order(int order,String orderName)
	{
		this.order = order;
		this.orderName = orderName;
	}
	int order;
	String orderName;
	
	public int getOrder() {
		return order;
	}
	
	public void setOrder(int order) {
		this.order = order;
	}
	
	public String getOrderName() {
		return orderName;
	}
	
	public void setOrderName(String orderName) {
		this.orderName = orderName;
	}
	
	@Override
	public boolean equals(Object obj) 
	{
		boolean flag = false;
		if (obj instanceof Order)
		{
			Order o = (Order) obj;
			if(this.order == o.order && this.orderName.equals(o.orderName))
			{
				flag = true;
			}
		}
		
		
		return flag;
	}
}
public class Test 
{
	public static void main(String[] args) 
	{
		Order o1 = new Order(123,"a001");
		Order o2 = new Order(123,"a001");
		System.out.println(o1.equals(o2));
	}
}
public class MyDate 
{
	public MyDate(int year,int month,int day)
	{
		this.year = year;
		this.month = month;
		this.day = day;
	}
	
	int year;
	int month;
	int day;
	
	@Override
	public boolean equals(Object obj)
	{
		int flag = 1;
		if(obj instanceof MyDate)
		{
			MyDate md = (MyDate) obj;
			flag = 0;
			
			if (this.year != md.year) {
				flag += 1;
			}
			
			if (this.month != md.month) {
				flag += 1;
			}
			
			if (this.day != md.day) {
				flag += 1;
			}
		}
		
		if (flag == 0) {
			return true;
		}else {
			return false;
		}
	}
}
	public static void main(String[] args) 
	{
//		Order o1 = new Order(123,"a001");
//		Order o2 = new Order(123,"a001");
//		System.out.println(o1.equals(o2));
		MyDate m1 = new MyDate(2019, 11, 23);
		MyDate m2 = new MyDate(2019, 11, 23);
		System.out.println(m1.equals(m2));
	}

3.包装类和toString:

在这里插入图片描述

		Integer i0 = 112;	//自动装箱
		int i1 = i0;	//自动拆箱		
		System.out.println(i0);
		//字符串转基本类型
		int i = Integer.parseInt("123");
		float f = Float.parseFloat("1.00");
		Boolean b = Boolean.parseBoolean("false");
		//基本类型转字符串
		String istr = String.valueOf(i);
		String ifloat = String.valueOf(f);
		String ib = String.valueOf(b);

4.关键字static:

让一个类的所有实例共享数据,就用类变量。
类变量不用实例化,直接类名.属性名可以使用

Chinese.country = "中国";
public class Chinese {
	//想知道new了几个对象
	public Chinese() {
		Chinese.count += 1;
	}
	static String country;
	String name;
	public static int count;
	int age;
	public static void showCount() {
		 System.out.println(Chinese.count);
	}
}
public class Test 
{
	public static void main(String[] args) {
//		Chinese.country = "中国";
//		Chinese c = new Chinese();
		c.country = "中国";
//		c.name = "xx";
//		c.age = 11;
//		Chinese c1 = new Chinese();
		c1.country = "中国";
//		c1.name = "xx";
//		c1.age = 11;
//		Chinese c2 = new Chinese();
 		c2.country = "中国";
//		c2.name = "xx";
//		c2.age = 11;
		Chinese c1 = new Chinese();
		Chinese c2 = new Chinese();
		Chinese c3 = new Chinese();
		Chinese.showCount();
	}
}

5.单例设计模式:

整个系统运行中,这个类只被实例化一次
节约资源和时间
饿汉式:

Single s = Single.getInstance();
Single s1 = Single.getInstance();
Single s2 = Single.getInstance();
Single s3 = Single.getInstance();
Single s4 = Single.getInstance();

在这里插入图片描述

6.main方法:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值