2019-7-6 [JavaSE] Object类 equals() finalize() toString() final 实现关系 抽象类 接口语法与特点

1.Object类:

equals() :

Object类是比较对象;String重写了;

class Student{//Object
	private int no;
	private String name;
	public Student(int no, String name) {
		this.no = no;
		this.name = name;
	}
	//重写
	@Override
	public boolean equals(Object obj) {
		// this , obj
		// this <->  (Student)obj
		Student stu = (Student)obj;
		boolean result = (this.no == stu.no && this.name.equals(stu.name));
		return result;
	}
	
	public String show() {
		return no + "," + name;
	}

	@Override // guojing.equals(yangkang);
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		// Student guojing    Teacher yangkang
		if (this.getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (this.name == null) {
			//yangkang
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (no != other.no)
			return false;
		return true;
	}
}
public class TestObject {
	public static void main(String[] args) {
		Student guojing = new Student(11, "郭靖");
		Student yangkang = new Student(11, "郭靖");
		System.out.println(guojing.equals(yangkang));//true
	}
}

finalize():清理资源。

垃圾回收器 在释放对象之前会调用。
//强制垃圾回收
System.gc();
Runtime.getRuntime().gc();

class Demo{

	@Override
	protected void finalize() throws Throwable {
		System.out.println("this---->" + this);//地址
	}
	
}
public class TestFinalize {

	public static void main(String[] args) {
		Demo demo = new Demo();
		System.out.println(demo);//地址
		demo = null;
		//强制垃圾回收
		System.gc();
		Runtime.getRuntime().gc();
		//上面两个一样的
		System.out.println(demo);

	}

toString():

使用print()或 println()输出对象名时,会隐式调用toString()的。、

class Employee{
	private int no;
	private String name;
	
	public Employee(int no, String name) {
		super();
		this.no = no;
		this.name = name;
	}

	@Override
	public String toString() {
		return no + "," + name;
	}
	
}
public class TestToString {

	public static void main(String[] args) {
		Employee guojing = new Employee(11,"郭靖");
		System.out.println(guojing);
    }
}

2.final

修饰:

类:不能被继承
方法:不能被重写
变量: 常量,值不能改。
名称规范:单词的所有字母都大写,多个单词组成用_下划线连接。
常量:

好处:

1.提高了安全性;
2.提高了可维护性;
3.提高了可读性;

final double PI = 3.1415926;
		double s = PI * 4 * 4;
		System.out.println(s);
		//引用:
		final Demo1 demo1 = new Demo1();
		demo1.value = 23;//对象的属性值可以改
		demo1 = new Demo1();// 对象不能改
		//引用:
		final int [] arr = new int [3];// 0,0,0
		arr[0] = 11;
		arr = new int[4];

设计可以被继承的类:

1.提供一个详细的文档说明;
2.继承的层次 通常 2 - 3层;
3.封装:
隐藏细节;
1 private 容量
2 public 容量 get(){}
3 protected void set(){}
4 final

3.实现关系

在这里插入图片描述
组合:整体类和局部类的关系。
语法:局部类作为整体类的成员变量;

/**
 * 局部
 */
class Heart{
	public void beat() {
		System.out.println("心跳");
	}
}
/**
 * 整体
 * */
class Wolf1{
//	Heart heart = new Heart();
	private Heart heart = new Heart();
	public void beat() {
		heart.beat();
	}
	public void run() {
		System.out.println("跑");
	}
}
public class TestAnimal1 {

	public static void main(String[] args) {
		Wolf1 wolf = new Wolf1();
		wolf.beat();
//		wolf.heart.beat();
		wolf.run();
	}
}

4.抽象类

抽象类:只定义了类的抽象行为,没有具体实现相应的行为。
在这里插入图片描述

注意:

1.抽象类不能创建对象;
2.抽象类中的抽象方法可有可无;
3.普通的子类 必须 重写实现抽象父类中的所有抽象方法;(抽象子类除外);
4.构造不能是抽象的;
5.抽象不能private ,static,final一起使用。

//抽象类
abstract class TrafficTool{
	private int count;
	public TrafficTool() {
	}
	public TrafficTool(int count) {
		this.count = count;
	}
	public int getCount() {
		return count;
	}
	public  abstract void run() ;//抽象方法
}
class Car1 extends TrafficTool{	
	public Car1(int count) {
		super(count);
	}
	public void run() {
		System.out.println("汽车在 公路上行驶,载客量:" + getCount() );
	}
}
class Tube extends TrafficTool{
	public void run() {
		System.out.println("地铁在隧道里行驶");
	}
}
public class TestTrafficTool {
	public static void main(String[] args) {
//		TrafficTool tool = new TrafficTool() ;//不能创建对象 
		Car1 car = new Car1(20);
		car.run();
//		Tube tube = new Tube();
//		tube.run();
	}
}

5.接口

接口:一组规则的封装。

5.1 语法

在这里插入图片描述

注意:

1)成员都是 public的;
2)常量 :public static final
3) 抽象方法:public abstract的
4)默认方法: public default

5.2 接口特点:

1.不能创建对象;
2.使用通过 实现类 来使用的;
3.实现类 实现接口 ,必须 实现所有的抽象方法;(抽象的实现类除外);
4.实现类可以实现 多个接口的功能;
5.接口之间可以继承,可以多继承。

示例一:

//三项电
interface ThreeElectric{
	void threeService();//抽象方法
}
//两项电
interface TwoElectric{
	 void twoService();
}
//插排
interface Socket extends ThreeElectric,TwoElectric{
	void socketService();//插排供电
}
class Goods{
	//商品
}
class Computer extends Goods implements Socket{
	@Override
	public void twoService() {
		System.out.println("计算机的外部设备两项电通电");
	}
	@Override
	public void threeService() {
		System.out.println("计算机本身三项电通电");
	}
	@Override
	public void socketService() {
		System.out.println("计算机连接插排供电");
	}

}
class Computer implements ThreeElectric,TwoElectric{
	@Override
	public void twoService() {
		System.out.println("计算机的外部设备两项电通电");
	}
	@Override
	public void threeService() {
		System.out.println("计算机本身三项电通电");
	}

}

public class TestElectric {

	public static void main(String[] args) {
		Computer computer = new Computer();
		computer.threeService();
		computer.twoService();
//		IceBox box = new IceBox();
//		box.threeService();
	}
}

class IceBox implements ThreeElectric{
	@Override
	public void threeService() {
		System.out.println("冰箱三项电通电");
	}	
}

示例二:

interface Info{
	double PI = 3.14;// public static final 
	void af();//public abstract 
	//默认方法 
	default void df() {
		System.out.println("df");
	}
	//接口中的静态方法:不能被子接口和实现类继承
	static void sf() {
		System.out.println("sf");
	}
}
class DemoImplInfo implements Info{
	public void show() {
		System.out.println(PI);
		df();
		Info.sf();
	}
	@Override
	public void af() {	
	}
}

示例三:

interface IA{
	int N = 11;//常量 static final 
	static void sf() {System.out.println("ia_sf");}
	default void df() {System.out.println("ia_df");}
	void af();
}
interface IB{
	int N = 22;//常量
	static void sf() {System.out.println("ib_sf");}
	default void df() {System.out.println("ib_df");}
	void af();
}
class DemoIAIB implements IA,IB{
	//匿名内部类分别是实现的
	IA ia = new IA() {
		@Override
		public void af() {
			System.out.println("ia_af");
		}
	};
	IB ib = new IB() {
		@Override
		public void af() {
			System.out.println("ib_af");
		}
	};
	@Override
	public void af() {
		System.out.println("af");
	}
	public void show() {
		System.out.println(IA.N);
		System.out.println(IB.N);
		IA.sf();
		IB.sf();
//		df();
	}
	@Override
	public void df() {
		IA.super.df();
		IB.super.df();
		System.out.println("hello");
	}

}

示例四:

interface WebBank{
	 void webService(); //简单
	public abstract void webService();//复杂,和上面一个意思
}
//实现类
class Jingdong implements WebBank{
	@Override
	public void webService() {
		System.out.println("京东实现了网上支付");
	}
}

class Taobao implements WebBank{
	@Override
	public void webService() {
		System.out.println("淘宝提供了网上支付功能");
	}
	
}
public class TestWebBank {

	public static void main(String[] args) {
		Jingdong jd = new Jingdong();
		jd.webService();
		Taobao taobao = new Taobao();
		taobao.webService();
	}

}

6.编写:利用组合 汽车的组成

class Engine{
	public void start() {System.out.println("发动机启动");}
	public void stop() {System.out.println("发动机停止");}
}
class Wheel{
	public void inflate(int n) {
		System.out.println("车轮充气:" + n);
	}
}
class Window{
	public void roolUp() {System.out.println("车窗升");}
	public void roolDown() {System.out.println("车窗降");}
}
class Door{
	Window win = new Window();
	public void open() {System.out.println("车门开");}
	public void close() {System.out.println("车门关");}
}
class Car{
	Engine engine;
//	Wheel w1 = new Wheel();
//	Wheel w2 = new Wheel();
//	Wheel w3 = new Wheel();
//	Wheel w4 = new Wheel();
	Wheel [] wheels;
	Door[] doors;
	{
		engine = new Engine();
		wheels = new Wheel[4];// null,null,null,null
		for(int i = 0; i < wheels.length; i++) {
			wheels[i] = new Wheel();
		}
		doors = new Door[2];
		for(int i = 0; i < doors.length; i++) {
			doors[i] = new Door();
		}
	}
}
public class TestCar_exam {

	public static void main(String[] args) {
		Car car = new Car();
		for(int i = 0; i < car.wheels.length; i++) {
			car.wheels[i].inflate(100);
		}
		car.doors[0].win.roolUp();
		car.doors[1].win.roolDown();
		car.doors[0].open();
		car.doors[1].close();
		car.engine.start();
		car.engine.stop();
	}
}
--------------------------------------
/1.5
class Engine{
	public void start() {System.out.println("启动");}
	public void stop() {System.out.println("停止");}
}
class Wheel{
	public void inflate(int load)
	{System.out.println("车轮充气"+load+"%");}
}
class Window{
	public void roolUp() {System.out.println("车窗升");}
	public void roolDown() {System.out.println("车窗降");}
}
class Door{ 
	Window window = new Window();
	public void open() {System.out.println("开门");}
	public void close() {System.out.println("关门");}
}
class Car{
	Engine engine = new Engine();
	Wheel wheel = new Wheel();
//	Wheel wheel2 = new Wheel();
//	Wheel wheel3 = new Wheel();
//	Wheel wheel4 = new Wheel();
	Door door = new Door();
//	Door door2 = new Door();
}

public class Demo5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Car car = new Car();
		System.out.println("四个车轮:");
		for (int i = 0; i < 4; i++) {
			car.wheel.inflate(100);
		}
		car.door.open();
		car.door.window.roolUp();
		car.door.close();
		car.door.window.roolDown();
		car.engine.start();
	}
}

7.编写:抽象类 职位的内容展示

abstract class Job{
	private String jobType;	
	public Job(String jobType) {
		this.jobType = jobType;
	}
	public String getJobType() {
		return jobType;
	}
	public abstract void showJob();//抽象方法
}
class CodeJob extends Job{
	private int codingLines;
	private int bugs;
	public CodeJob(String jobType,int codingLines,int bugs) {
		super(jobType);//调用父类构造
		this.codingLines = codingLines;
		this.bugs = bugs;
	}
	@Override
	public void showJob() {
		System.out.println("我是一名" 
		+ getJobType() 
		+ ",每天的代码行数" 
		+ codingLines 
		+ ",错误数:"
		+ bugs);
	}	
}

public class TestJob_exam {

	public static void main(String[] args) {
		CodeJob job = new CodeJob("软件开发工程师", 1000, 2);
		job.showJob();
	}
}

8.编写:接口 实现书信接口

interface Letter{
	public abstract void writeHeader();
	void writeBody();
	void writeFooter();
}
class HomeLetter implements Letter{
	@Override
	public void writeHeader() {
		System.out.println("亲爱的父母:");
	}
	@Override
	public void writeBody() {
		System.out.println("我在北京一切都好,每天都在努力的学习,同学们非常好,请放心!");
	}
	@Override
	public void writeFooter() {
		System.out.println("\t\t\t\t郭靖 2019-7-6");
	}	
}
public class TestLetter_exam {

	public static void main(String[] args) {
		HomeLetter letter = new HomeLetter();
		letter.writeHeader();
		letter.writeBody();
		letter.writeFooter();
	}
}

9.java的垃圾回收机制分析

java的垃圾回收机制篇幅过大,另起一篇专门介绍

10.Hashcode算法研究

实例:

public int hashCode() {
		final int prime = 31;
		int result = 1;
		// 31 * 
		result = prime * result + no;
		return result;
	}
}
public class TestHashCode {

	public static void main(String[] args) {
		Book book = new Book(11, "计算机基础");
		System.out.println(book.hashCode());		
		String str = "ab";// char [] value = {'a','b'};
		System.out.println(str.hashCode());//hash编码结果3105
	}
}

JDK 中, String 类型是如何重写 hashCode 方法的呢?
源码:

 public int hashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;
            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }

代码非常简单,就是使用 String 的 char 数组的数字每次乘以 31 再叠加最后返回,因此,每个不同的字符串,返回的 hashCode 肯定不一样。
那么为什么使用 31 呢?
在名著 《Effective Java》第 42 页就有对 hashCode 为什么采用 31 做了说明:
之所以使用 31, 是因为他是一个奇素数。
如果乘数是偶数,并且乘法溢出的话,信息就会丢失,因为与2相乘等价于移位运算(低位补0)。
31 有个很好的性能,即用移位和减法来代替乘法,可以得到更好的性能:
31 * i == (i << 5) - i,
现代的JVM 可以自动完成这种优化。
使用 31 最主要的还是为了性能。当然用 63 也可以。但是 63 的溢出风险就更大了。

11.注意:

1.黄色是protect修饰的
绿色的是private修饰的
2.强制垃圾回收,
System.gc(); Runtime.getRuntime().gc();
这两个是通知垃圾回收过来回收
3.课下看看java垃圾回收咋回事的
4.hascode算法
5.final主要是让被修饰的东西不可被修改
6.final修饰的对象不能修改,但对象的属性值可以修改
7.抽象父类定义的抽象方法,那么子类必须重写这个方法。
8.普通类之中不可以定义抽象方法;只有抽象类才可以定义抽象方法
9.抽象方法不能pricate,static,final一起使用
10.接口:不能创建对象;使用是通过实现类来使用的;实现类实现接口的功能,必须实现所有的抽象方法;抽象的实现类除外;实现类可以实现多个接口的功能;接口之间可以继承,可以多继承.。
11.在接口里void webServiece();
相当于
public abstract void webServiece();
12.在接口里使用implemwents关键字可以继承接口定义的功能
13.接口中的静态方法不能被子接口和实现类所继承

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值