p523 作业
public class Computer {
private InsertDrawable insertDrawable;
public Computer(InsertDrawable insertDrawable) {
this.insertDrawable = insertDrawable;
}
public Computer() {
}
public void setInsertDrawable(InsertDrawable insertDrawable) {
this.insertDrawable = insertDrawable;
}
public InsertDrawable getInsertDrawable() {
return insertDrawable;
}
public void operate(){
InsertDrawable insertDrawable = this.getInsertDrawable();
insertDrawable.connect();
insertDrawable.disconnect();
}
}
public class KeyBoard extends Computer implements InsertDrawable{
public void connect(){
System.out.println("键盘连接");
}
public void disconnect(){
System.out.println("键盘断开");
}
}
public class Mouse extends Computer implements InsertDrawable {
public void connect(){
System.out.println("鼠标连接");
}
public void disconnect(){
System.out.println("鼠标断开");
}
}
public class Printer extends Computer implements InsertDrawable{
@Override
public void connect() {
System.out.println("连接打印机");
}
public void disconnect() {
System.out.println("断开打印机");
}
}
public class Window extends Computer implements InsertDrawable{
public void connect(){
System.out.println("连接显示器");
}
public void disconnect(){
System.out.println("断开显示器");
}
}
public interface InsertDrawable {
void connect();
void disconnect();
}
public class Test{
public static void main(String[] args) {
//多态,创建接口对象
InsertDrawable divice1 = new KeyBoard();
InsertDrawable divice2 = new Mouse();
InsertDrawable divice3 = new Window();
InsertDrawable divice4 = new Printer();
//创建电脑对象
Computer c1 = new Computer(divice1);
Computer c2 = new Computer(divice2);
Computer c3 = new Computer(divice3);
Computer c4 = new Computer(divice4);
//电脑操作
c1.operate();
System.out.println("_____________________");
c2.operate();
System.out.println("_____________________");
c3.operate();
System.out.println("_____________________");
c4.operate();
}
}
p524 访问控制权限
p524 访问控制权限可以修饰什么?
p525访问控制权限可以修饰什么?
面向对象结束,往后都是调用接口
p526源码以及api
**看一下JDK的根类:Obiect类 **
p527-528 Object的toString方法,toString方法都是要重写的
public class Object_toString {
public static void main(String[] args) {
MyTime t1 = new MyTime(1997,04,10);
// String s1 = t1.toString();
//重写toString之前
System.out.println(t1.toString());
//重写toString之后
System.out.println(t1.toString());
}
}
class MyTime{
int year;
int month;
int day;
public MyTime() {
}
public MyTime(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
@Override
public String toString() {
/*return "MyTime{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';*/
// return this.year+"年"+this.month+"月"+this.day+"日";
return this.year+"/"+this.month+"/"+this.day;
}
}
p529-p531 Object的equals都是要重写的
public class Object_toString {
public static void main(String[] args) {
MyTime t1 = new MyTime(1997,04,10);
MyTime t2 = new MyTime(1997,04,10);
MyTime t3 = new MyTime(2020,12,27);
// String s1 = t1.toString();
//重写toString之前
System.out.println(t1.toString());
//重写toString之后
System.out.println(t1.toString());
//重写equals之前。默认使用“==”比较。
boolean flag = t1.equals(t2);
System.out.println(flag);
boolean flag2 = t1.equals(t3);
System.out.println(flag2);
}
}
class MyTime{
int year;
int month;
int day;
public MyTime() {
}
public MyTime(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
@Override
public String toString() {
/*return "MyTime{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';*/
// return this.year+"年"+this.month+"月"+this.day+"日";
return this.year+"/"+this.month+"/"+this.day;
}
//重写equals
/*public boolean equals(Object obj){
int year1 = this.year;
int month1 = this.month;
int day1 = this.day;
if(obj instanceof MyTime){
MyTime t = (MyTime)obj;
int year2 = t.year;
int month2 = t.month;
int day2 = t.day;
if (year1 == year2 && month1 == month2 && day1 == day2){
return true;
}
}
return false;
}*/
//改进equals
// public boolean equals(Object obj){
// //obj为空,直接返回false
// if(obj == null){
// return false;
// }
// //obj不属于MyTime类型,直接返回false
// if(!(obj instanceof MyTime)){
// return false;
// }
// //如果this和obj指向同一个内存地址,则说明相同,返回true
// if(this == obj){
// return true;
// }
// //程序执行到这里,说明obj是MyTime类型而且不为空
// MyTime t = (MyTime)obj;
// if (this.year == t.year && this.month == t.month && this.day == t.day){
// return true;
// }
//
// return false;
// }
//再次改进
public boolean equals(Object obj){
//obj为空,直接返回false
if(!(obj instanceof MyTime)){
return false;
}
//如果this和obj指向同一个内存地址,则说明相同,返回true
if(this == obj){
return true;
}
//程序执行到这里,说明obj是MyTime类型而且不为空
MyTime t = (MyTime)obj;
return this.year == t.year && this.month == t.month && this.day == t.day;
p532 使用idea工具进行方法重写
import java.util.Objects;
public class IdeaGenerator {
private int year;
private int month;
private int day;
public IdeaGenerator(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public IdeaGenerator() {
}
public void setYear(int year) {
this.year = year;
}
public void setMonth(int month) {
this.month = month;
}
public void setDay(int day) {
this.day = day;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
@Override
public String toString() {
return "IdeaGenerator{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
IdeaGenerator that = (IdeaGenerator) o;
return year == that.year && month == that.month && day == that.day;
}
}
p533 重写equals方法,toString方法练习
import java.util.Objects;
public class equalsTest02 {
public static void main(String[] args) {
Student s1 = new Student(1997,"西安理工大学");
Student s2 = new Student(1997,"西安理工大学");
Student s3 = new Student(1997,"兰州城市学院");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
}
}
class Student{
int no;
String school;
public Student() {
}
public Student(int no, String school) {
this.no = no;
this.school = school;
}
@Override
public String toString() {
return
"学号:" + no +
", 学校:'" + school ;
}
@Override
public boolean equals(Object obj) {
if(obj == null || !(obj instanceof Student)) return false;
if(this == obj) return true;
Student s = (Student) obj;
return this.no == s.no && this.school.equals(s.school);
}
}
p534 重写equals总结
p535-p536 equals方法深层次理解
重写equals方法要彻底,调用的所有类都要重写equals
import java.util.Objects;
public class equalsTest03 {
public static void main(String[] args) {
Address addr1 = new Address("武威市","永昌镇","733000");
Address addr2 = new Address("武威市","永昌镇","733000");
User u1 = new User("ZhonejieMa",addr1);
User u2 = new User("ZhonejieMa",addr2);
User u3 = new User("ZhonejieMa",new Address("武威市","永昌镇","733000"));
User u4 = new User("ZhonejieMa",new Address("金昌市","永昌镇","733000"));
System.out.println(u1.equals(u2));
System.out.println(u1.equals(u3));
System.out.println(u1.equals(u4));
}
}
class User{
String name;
Address addr;
public User() {
}
public User(String name, Address addr) {
this.name = name;
this.addr = addr;
}
//重写equals方法
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof User)) return false;
if (this == obj) return true;
User user = (User) obj;
return this.name.equals(user.name) && this.addr.equals(user.addr);
}
}
class Address{
String city;
String street;
String zipcode;
public Address() {
}
public Address(String city, String street, String zipcode) {
this.city = city;
this.street = street;
this.zipcode = zipcode;
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof Address)) return false;
if (this == obj) return true;
Address address = (Address) obj;
return this.city == address.city
&& this.street == address.street
&& this.zipcode == address.zipcode;
}
}
p537 Object类的finalize()方法【非重点,了解即可】
public class finalizeTest {
public static void main(String[] args) {
/*Person p = new Person();
p = null;//垃圾回收机制没启动*/
//多造点垃圾
/* for (int i = 0;i < 1000000;i++){
Person p = new Person();
p = null;
}*/
//垃圾够多时,就会启动垃圾回收机制
//代码建议启动垃圾回收机制,但是有可能不启动
for (int i = 0;i<1000;i++){
Person p = new Person();
p = null;
System.gc();
}
}
}
class Person{
@Override
protected void finalize() throws Throwable {
//this代表当前对象
System.out.println(this+"即将被销毁!");
}
}
p538 Object类的HashCode()
简单看一下输出结果就行,以后学习
public class hashCodeTest {
public static void main(String[] args) {
Object o = new Object();
int hashCodeValue = o.hashCode();
System.out.println(hashCodeValue);
MyClass mc = new MyClass();
int hashCodeValue2 = mc.hashCode();
System.out.println(hashCodeValue2);
}
}
class MyClass{
}
p538 匿名内部类概述
p539 引出匿名内部类
public class innerClassTest {
public static void main(String[] args) {
MyMath mm = new MyMath();
// mm.MySum(new ComputeImpl(),100,200);
Compute c = new ComputeImpl();
mm.MySum(c, 100, 50);
}
}
//负责计算的接口
interface Compute{
int sum(int a ,int b);
}
//接口的实现类
class ComputeImpl implements Compute{
//这就是接口的实现
public int sum(int a,int b){
return a+b;
}
}
//数学类
class MyMath{
//数学类求和方法
public void MySum(Compute c ,int x ,int y){
int retValue = c.sum(x,y);
System.out.println(x+"+"+y+"="+retValue);
}
}
p540 匿名内部类详解
ALT+回车————————————————————纠错键