本篇文章是根据视频学的,来源如下所示:
教程来源:
java基础到高级_零基础自学Java–尚硅谷–宋红康java基础到高级_零基础自学Java--尚硅谷--宋红康_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1ny4y1Y7CW
目录
1.Eclipse常用快捷键1
2..Eclipse常用快捷键2
3.Eclipse常用快捷键3
4.回顾Bank练习的难点
5.项目二:功能演示
6.项目二:软件结构设计
7.项目二:CMUtility工具类的功能介绍
8.项目二:Customer类的设计
9.项目二:CustomerList类的设计
10.项目二:CustomerView的整体框架的构建
11.项目二:CustomerView的退出功能的实现
12.项目二:CustomerView的客户列表功能的实现
13.项目二:CustomerView添加客户功能的实现
14.项目二:CustomerView修改客户功能的实现
15.项目二:CustomerView删除客户功能的实现
16.项目二:总结
17.继承性的理解
18.继承性的使用
19.继承性的再说明
20.Object类的理解
20.每日一考
21.继承性练习1:基本 *** 作
22.继承性练习2:基本 *** 作
23.EclipseDebug的使用
24.项目二中使用Debug调试
25.解决Debug中step into功能失灵的问题
27.方法重写的细节
28.方法重写的练习
29.测试4中不同的权限修饰
30.super调用属性和方法---super爱豆的笑容
31.super调用构造器
32.子类对象实例化的全过程
33.继承和super的课后练习
34.多态性的使用------比较难,比较重要的(老师原话)(Polymorphism)
35.多态性使用举例
36.多态性不适用于属性
37.虚拟方法调用的再理解
38.每日一考
39.向下转型的使用
40.instanceof关键字的使用
41.向下转型的几个常见的问题
42.多态练习:调用属性和方法
43.多态性练习:基本 *** 作
44.多态性练习:几何图形
45.多态性练习:重写方法
46.Object类结构的解析
47.==运算符的回顾
48.equals()的使用
49.重写equals()
50.总结equals()与==
51.equals()练习1:代码实现
52.equals()练习2:代码实现
53.toString()的使用
54.Object类综合练习使用
55.单元测试方法的使用
56.包装类的理解
57.基本数据类型转换为包装类
58.包装类转换为基本数据类型
59.新特性:自动装箱与自动拆箱
60.基本数据类型包装类与String的相互转换
61.包装类常见面试问题
62.包装类的课后练习
63.每日一考
1.Eclipse常用快捷键1 2..Eclipse常用快捷键2 3.Eclipse常用快捷键3
- 补全代码:Alt + /或者改为Windows-----Preference------java-------Editor------Content Assist-------trigger for java之中的内容改为qwertyuiopasdfghjklzxcvbnm.,那么就是任何内容都是提示的了
- 修改快捷键的方式:Windows - Preference - 搜索(keys) - 修改
package com.atshangguigu.java; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; public class EclipseKeys { final double pi = 3.14;//这里表明他是一个常量 public static void main(String[] args) { String s = new String(); String str = new String(); ArrayList list = new ArrayList(); HashMap map = null; int map1 = 12; map1 = 18; Date date = new Date(123456); } } class User { private int Id; private String name public int getId() { return Id; } public void setId(int id) { Id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public User(int id, String name) { Id = id; this.name = name; }; }4.回顾Bank练习的难点
- 客户信息管理软件
- 详见视频
- 学生继承人类(代码如下所示)
- Person.java
package com.atshangguigu.java; public class Person { String name; int age; public Person() { } public Person(String name,int age) { this.name = name; this.age = age; } public void eat() { System.out.println("还有一个多小时我就可以去吃饭了"); } public void sleep() { System.out.println("今天中午睡到了下午两点半"); } }
- Student.java
package com.atshangguigu.java; public class Student extends Person{//这里使用extends Person那么我们就可以直接继承Person之中的东西 //String name; //int age; String major; public Student() { } public Student(String name,int age,String major) { this.name = name; this.age = age; this.major = major; } public void study() { System.out.println("小杨在努力学习Java呀!"); } }
- ExtendTest.java
package com.atshangguigu.java; public class ExtendTest { public static void main(String[] args) { Person p1 = new Person(); p1.age = 1; p1.eat(); Student s1 = new Student(); s1.eat(); s1.sleep(); s1.name = "Yang"; } }18.继承性的使用
- Person.java
package com.atshangguigu.java; public class Person { String name; private int age; public Person() { } public Person(String name,int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void eat() { sleep(); System.out.println("还有一个多小时我就可以去吃饭了"); } private void sleep() { System.out.println("今天中午睡到了下午两点半"); } }
- Student.java
package com.atshangguigu.java; public class Student extends Person{//这里使用extends Person那么我们就可以直接继承Person之中的东西 //String name; //int age; String major; public Student() { } public Student(String name,int age,String major) { this.name = name; setAge(age); this.major = major; } public void study() { System.out.println("小杨在努力学习Java呀!"); } public void show() { System.out.println("name:" + name + ",age:" + getAge()); } }
- ExtendTest.java
package com.atshangguigu.java; public class ExtendTest { public static void main(String[] args) { Person p1 = new Person(); //p1.age = 1; p1.eat(); Student s1 = new Student(); s1.eat(); //s1.sleep(); s1.name = "Yang"; s1.setAge(10); System.out.println(s1.getAge()); } }19.继承性的再说明
* 三.java中关于继承性的一些规定
1.一个类可以被多个子类继承.(一个人可以生多个孩子)
2.java之中的单继承性:一个类只能有一个父类.(吕布:你再骂?)[C++之中可以有多个父类]
3.子父类是一个相对的概念.多层继承(祖爷爷-爷爷-爸爸-儿子:四世同堂-----基因的角度)
4.子类直接继承的父类称为直接父类,间接继承的父类称为:间接父类
5.子类继承父亲以后,就直接获取了父类以及间接父类之中的所有的已经声明的属性以及方法.
- 创建一个类,之后直接调用------未知的继承父类("我爸爸是谁?")
- Creature s = new Creature(); s.-----可以知道,.后面自己产生相应的函数.
- 四.
1.如果我们没有显式声明一个类的父亲的话,则直接继承java.lang.Object类
2.所有的java类(除了java.lang.Object类之外)都是直接或者间接的继承于java.lang.Object类
3.意味着,所有的java类具有java.lang.Object类声明的功能.
- 箭头:子类指向父类
package day12; public class ManKind { //属性 private int sex; private int salary; //构造器 public ManKind() { } public ManKind(int sex, int salary) { super(); this.sex = sex; this.salary = salary; } //方法 void manOrWoman() { if(sex == 1) { System.out.println("man"); } else if(sex == 0) { System.out.println("woman"); } else { System.out.println("中性"); } } void employeed() { String job = ((salary == 0)?"no job":"job"); System.out.println(job); } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } }
package day12; // 这里javabean的条件是1.类是公共的 2.无参构造器 3.私有的属性(get与set的方法) // [后面在反射的过程之中会提及] public class Kid extends ManKind{ //属性 private int yearsOld; //构造器 public Kid() { } public Kid(int yearsOld) { this.yearsOld = yearsOld; } //方法 public void printAge() { System.out.println("I am " + yearsOld + "years old "); } public int getYearsOld() { return yearsOld; } public void setYearsOld(int yearsOld) { this.yearsOld = yearsOld; } }
package day12; public class KidTest { public static void main(String[] args) { Kid somekid = new Kid(12); somekid.printAge(); somekid.setSex(0); somekid.setSalary(18); somekid.manOrWoman(); somekid.employeed(); } }22.继承性练习2:基本 *** 作
- 下面是博主按照自己的理解写的,可能与视频有一些出入.
package com.atshangguigu.exer; public class Circle { private double radius;//半径 public Circle() { radius = 1.0; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } //返回圆的半径 public double findArea() { return Math.PI * getRadius() * getRadius(); } }
package com.atshangguigu.exer; public class Cylinder extends Circle{ private double length; Cylinder() { length = 1.0; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } double findVolumn() { return getLength()*findArea(); } }
package com.atshangguigu.exer; public class FinalTest { public static void main(String[] args) { Cylinder test = new Cylinder(); System.out.println(test.findVolumn()); test.setRadius(58.2); test.setLength(12); System.out.println(test.findVolumn()); } }23.EclipseDebug的使用
- 在实际开发之中会用的比较多
- 如何调试程序:
1. System.out.println().
2. Eclipse - Debug调试 - 加入断点的方法:双击(断点可以理解成为一个个小的关卡,在程序之中我们可以设置多个断点)----右键(点击debug as ):显示小臭虫界面
- 相应的界面如下所示:
- Drop to frame:到达首行的位置
- 没有进入方法内部:原因是Debug Configurations --- JRE(jdk1.8.0)->具体过程见视频
package com.atshangguigu.exer1; public class Person { String name; int age; public Person() {} public Person(String name,int age) { this.name = name; this.age = age; } public void eat() { System.out.println("吃饭"); } public void walk(int distance) { System.out.println("我今天走了" + distance + "公里"); } }
package com.atshangguigu.exer1; public class Student extends Person { String major; public Student() {} public Student(String major) { this.major = major; } public void study() { System.out.println("我爱天津"); } public void eat() { System.out.println("学生一顿吃两份米饭,因为学五食堂的米饭太少了"); } }
package com.atshangguigu.exer1; public class StudentTest { public static void main(String[] args) { Student s = new Student("仪器仪表工程"); s.eat(); s.walk(18); s.study(); } }
- 面试题:区分方法的重载与重写
- Person.java
package com.atshangguigu.exer1; public class Person { String name; int age; public Person() {} public Person(String name,int age) { this.name = name; this.age = age; } //对其进行重写 public void eat() { System.out.println("吃饭"); } public void walk(int distance) { System.out.println("我今天走了" + distance + "公里"); eat(); show(); } private void show()//私有方法 { System.out.println("我不是一个好人呀"); } public Object info() { return null; } public double info1() { return 10.0; } }
- Student.java
package com.atshangguigu.exer1; public class Student extends Person { String major; public Student() {} public Student(String major) { this.major = major; } public void study() { System.out.println("我爱天津"); } public void eat() { System.out.println("学生一顿吃两份米饭,因为学五食堂的米饭太少了"); } //这里是不能够重写的,因为这里前面可以见到没有一个绿三角,说明没有重写 public void show()//私有方法 { System.out.println("我是一个好学生"); } public String info()//这里的含义是我们将父类之中的Object改为了String { return null; } }
- StudentTest.java
package com.atshangguigu.exer1; public class StudentTest { public static void main(String[] args) { Student s = new Student("仪器仪表工程"); s.eat(); s.walk(18);//通过这句话证明子类不能够重写父类之中的priate的方法 s.study(); } }28.方法重写的练习
- 上面的第二题是非常简单的,这里的代码就不重复粘贴了.代码量比较大,没有什么意义.
- 如上所示,在Browse之中可以直接改掉新建的class的继承的父类.
- java2下面的
package com.atshangguigu.java2; public class Order { private int orderPrivate; int orderDefault; protected int orderProtected; public int orderPublic; private void methodPrivate() { orderPrivate = 1; orderDefault = 2; orderProtected = 3; orderPublic = 4; } void methodDefault() { orderPrivate = 1; orderDefault = 2; orderProtected = 3; orderPublic = 4; } protected void methodProtected() { orderPrivate = 1; orderDefault = 2; orderProtected = 3; orderPublic = 4; } public void methodPublic() { orderPrivate = 1; orderDefault = 2; orderProtected = 3; orderPublic = 4; } }
- java3下面的SubOrder.java
package com.atshangguigu.java3; import com.atshangguigu.java2.Order; public class SubOrder extends Order { public void method() { orderProtected = 1; orderPublic = 2; methodProtected(); methodPublic(); } }
- java3下面的OrderTest.java
package com.atshangguigu.java3; import com.atshangguigu.java2.Order;//ctrl + shift + o是进行导包的一个过程 public class OrderTest { public static void main(String[] args) { Order order = new Order(); //不同包下的普通类要是想要调用Order之中的属性与方法,只能调用的是public类,其余的什么private 缺省 protected是调用不了的 //order.orderPrivate = 1; // order.orderDefault = 2; // order.orderProtected = 3; order.orderPublic = 4; // order.methodDefault(); // order.methodProtected(); order.methodPublic(); } }30.super调用属性和方法---super爱豆的笑容
- 属性是没有覆盖的,方法是有的
- d幕:super是this的爹
- Person.java
package com.atshangguigu.java4; public class Person { String name; int age; int id = 1;//身份z号 public Person() { } public Person(String name) { this.name = name; } public Person(String name,int age) { this(name); this.age = age; } public void eat() { System.out.println("还有一个小时就要去吃饭了"); } public void walk() { System.out.println("生下来就会跑"); } }
- student.java
package com.atshangguigu.java4; public class Student extends Person{ String major; int id = 20212023;//学号 public Student() {} public Student(String major) { this.major = major; } @Override public void eat() { super.eat(); System.out.println("今天去学四食堂吃米饭,还是去学五食堂吃麻辣烫?"); } public void study() { System.out.println("今天晚上有一节激光测量技术课"); } public void show() { System.out.println("name = " + super.name + ",age = " + this.age); System.out.println("学号是" + id); System.out.println("身份z号是" + super.id); } }
- SuperTest.java
package com.atshangguigu.java4; public class SuperTest { public static void main(String[] args) { Student stu = new Student(); stu.show(); stu.study(); stu.eat(); } }31.super调用构造器
- Person.java
package com.atshangguigu.java4; public class Person { String name; int age; int id = 1;//身份z号 public Person() { System.out.println("我是猪八"); } public Person(String name) { this.name = name; } public Person(String name,int age) { this(name); this.age = age; } public void eat() { System.out.println("还有一个小时就要去吃饭了"); } public void walk() { System.out.println("生下来就会跑"); } }
- Student.java
package com.atshangguigu.java4; public class Student extends Person{ String major; int id = 20212023;//学号 public Student() { } public Student(String major) { this.major = major; } public Student(String name,int age,String major) { super(name,age); this.major = major; } @Override public void eat() { super.eat(); System.out.println("今天去学四食堂吃米饭,还是去学五食堂吃麻辣烫?"); } public void study() { System.out.println("今天晚上有一节激光测量技术课"); } public void show() { System.out.println("name = " + this.name + ",age = " + this.age); System.out.println("学号是" + id); System.out.println("身份z号是" + super.id); } }
- SuperTest.java
package com.atshangguigu.java4; public class SuperTest { public static void main(String[] args) { Student stu = new Student(); stu.show(); stu.study(); stu.eat(); Student s1 = new Student("Yang",23,"仪器工程 哼!"); s1.show(); System.out.println("*-*-*-*--*-*-*-*-*-*--*-*-*-*-*-"); Student s2 = new Student(); } }32.子类对象实例化的全过程
- * 子类对象实例化的全过程
* 1.从结果上看:(继承性)
* 子类继承父类以后,就获取了父类中声明的属性和方法
* 创建子类对象,在堆空间之中,就会加载所有父类中的声明的属性
* 2.从过程上看:
* 当我们通过子类的构造器创建对象的时候,我们一定会直接或者间接的调用父类构造器,
* 进而调用父类的父类的构造器,因为加载过所有父类的结构,所以才可以看到内存之中有
* 父类的结构,子类对象才可以进行调用.
* 明确:虽然创建子类对象时,调用了父类的构造器,但是只是创建过一个对象,也就是那个new 出来的对象.
- Account.java
package com.atshangguigu.java6; public class Account { private int id;//账号 private double balance;//存款 private double annualInterestRate;//年利率 //构造器 public Account(int id, double balance, double annualInterestRate) { super(); this.id = id; this.balance = balance; this.annualInterestRate = annualInterestRate; } public int getId() { return id; } public void setId(int id) { this.id = id; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public double getAnnualInterestRate() { return annualInterestRate; } public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; } //月利率 public double getMonthlyInterest() { return annualInterestRate / 12; } //取钱 public void withdraw (double amount) { if (balance >= amount) { balance -= amount; return ; } else{ System.out.println("余额不足,没钱了"); return ; } } //存钱 public void deposit (double amount) { balance += amount; } }
- AccountTest.java
package com.atshangguigu.java6; public class AccountTest { public static void main(String[] args) { Account test = new Account(1122,20000,0.045); test.withdraw(30000); System.out.println("您的账户余额为" + test.getBalance()); test.withdraw(2500); test.deposit(3000); System.out.println("您的账户余额为" + test.getBalance() + " 当前账户的月利率为" + 100*test.getMonthlyInterest() + "%"); } }
- CheckAccount.java
package com.atshangguigu.java6; public class CheckAccount extends Account{ private double overdraft;//可透支限额 public CheckAccount(int id, double balance, double annualInterestRate,double overdraft) { super(id,balance,annualInterestRate); this.overdraft = overdraft; } public double getOverdraft() { return overdraft; } public void setOverdraft(double overdraft) { this.overdraft = overdraft; } @Override public void withdraw(double amount) { if(getBalance() >= amount ) { //方式一 //setBalance(getBalance() - amount); //方式二 super.withdraw(amount); } else if(overdraft >= (amount - getBalance()))//透支额度加上余额是足够的 { overdraft -= (amount - getBalance()); setBalance(0); //或者是 super.withdraw(getBalance()); } else { System.out.println("超过可以透支的限额"); } } }
- CheckAccountTest.java
package com.atshangguigu.java6; public class CheckAccountTest { public static void main(String[] args) { CheckAccount acc = new CheckAccount(1122,20000,0.045,5000); acc.withdraw(5000); System.out.println("您的账户余额为:" + acc.getBalance()); System.out.println("您的可透支额度为:" + acc.getOverdraft()); acc.withdraw(18000); System.out.println("您的账户余额为:" + acc.getBalance()); System.out.println("您的可透支额度为:" + acc.getOverdraft()); acc.withdraw(3000); System.out.println("您的账户余额为:" + acc.getBalance()); System.out.println("您的可透支额度为:" + acc.getOverdraft()); } }34.多态性的使用------比较难,比较重要的(老师原话)(Polymorphism)
- Person.java
package com.atshangguigu.java7; public class Person { String name; int age; public void eat() { System.out.println("生而为猪,我很能吃"); } public void walk() { System.out.println("见过猪跑没"); } }
- Man.java
package com.atshangguigu.java7; public class Man extends Person{ boolean isMoking; public void earnMoney() { System.out.println("挣钱,娶老婆"); } public void eat() { System.out.println("多吃饭"); } public void walk() { System.out.println("删库,跑路"); } public void isSmoking() { system.out.println("男生可以抽烟"); } }
- Woman.java
package com.atshangguigu.java7; public class Woman extends Person{ boolean isBeauty; public void goShopping() { System.out.println("女生喜欢购物"); } public void eat() { System.out.println("不吃饭"); } public void walk() { System.out.println("走路"); } }
- PersonTest.java
package com.atshangguigu.java7; public class PersonTest { public static void main(String[] args) { Person p1 = new Person(); p1.eat(); Man man = new Man(); man.eat(); man.age = 23; man.earnMoney(); / public class PersonTest { public static void main(String[] args) { Person p1 = new Person(); p1.eat(); Man man = new Man(); man.eat(); man.age = 23; man.earnMoney(); / if(p2 instanceof Woman) { Woman w1 = (Woman)p2; w1.goShopping(); System.out.println("p2是一个Woman"); } else { System.out.println("p2不是一个Woman"); } if(p2 instanceof Man) { System.out.println("p2是Man"); } else { System.out.println("p2不是Man"); } if(p2 instanceof Person) { System.out.println("p2是Person"); } else { System.out.println("p2不是Person"); } if(p2 instanceof Object) { System.out.println(" Yes "); } else { System.out.println("No"); } } }41.向下转型的几个常见的问题
package com.atshangguigu.java; public class PersonTest { public static void main(String[] args) { Person p1 = new Person(); p1.eat(); Man man = new Man(); man.eat(); man.age = 23; man.earnMoney(); / if(p2 instanceof Woman) { Woman w1 = (Woman)p2; w1.goShopping(); System.out.println("p2是一个Woman"); } else { System.out.println("p2不是一个Woman"); } if(p2 instanceof Man) { System.out.println("p2是Man"); } else { System.out.println("p2不是Man"); } if(p2 instanceof Person) { System.out.println("p2是Person"); } else { System.out.println("p2不是Person"); } if(p2 instanceof Object) { System.out.println(" Yes "); } else { System.out.println("No"); } //练习: //问题一:编译的时候通过,运行的时候不通过 //举例一 // Person p3 = new Woman(); // Man m2 = (Man)p3; //举例二 // Person p4 = new Person();//想想也不合适 // Man m4 = (Man) p4; // m4.earnMoney(); // //问题2:编译时候通过,运行的时候也是通过的 Object obj = new Woman(); Person p = (Person)obj; //问题三:编译不通过 // Man m5 = new Woman(); // String str = new Data(); } }42.多态练习:调用属性和方法
package com.atshangguigu.Exer; public class FieldMethodTest { public static void main(String[] args) { Sub s = new Sub(); System.out.println(s.count);//20:这里调用的是Sub之中的方法:就近原则的存在 s.display();//20,这里的display()重写了,但是还是执行的是子类之中的代码 base b = s;//将s的地址值赋给b System.out.println(b == s);//后面会==进行详细的介绍,这里比较的是二者的地址值是否相同 System.out.println(b.count);//这里是属性,是不是具有多态性的 b.display();//20这里的是方法,是具有多态性的------虚拟方法的调用 } } class base { //属性 int count = 10; public void display() { System.out.println(this.count); } } class Sub extends base { //属性:不体现多态性 int count = 20; public void display() { System.out.println(this.count); } }
-
这里需要注意:同名的属性和同名的方法
-
总结:
1.若子类重写了父类方法,就意味着子类里定义的方法彻底覆盖了父类里的 同名方法,系统将不可能把父类里的方法转移到子类中. 2.对于实例变量则不存在这样的现象,即使子类里定义了与父类完全相同的 实例变量,这个实例变量依然不可能覆盖父类中定义的实例变量.(属性) - 编译看左边,运行看右边
package com.atshangguigu.Exer; public class InstanceTest { public static void main(String[] args) { InstanceTest test = new InstanceTest(); test.method(new Student());//虚拟的方法调用 } public void method(Person e) { //虚拟方法的调用 String info = e.getInfo(); System.out.println(info); //方式一 if(e instanceof Graduate) { System.out.println("a graduated student"); System.out.println("a student"); System.out.println("a person"); } else if(e instanceof Student) { System.out.println("a student"); System.out.println("a person"); } else if(e instanceof Person) { System.out.println("a person"); } //方式二 if(e instanceof Graduate) { System.out.println("a graduated student"); } if(e instanceof Student) { System.out.println("a student"); } if(e instanceof Person) { System.out.println("a person"); } } } class Person { protected String name = "person"; protected int age = 50; public String getInfo() { return "Name: " + name + "n" + "age: " + age; } } class Student extends Person { protected String school = "pku"; public String getInfo() { return "Name: " + name + "nage: " + age + "nschool: " + school; } } class Graduate extends Student { public String major = "IT"; public String getInfo() { return "Name: " + name + "nage: " + age + "nschool: " + school + "nmajor:" + major; } }44.多态性练习:几何图形
- 练习如下所示:
- 注意:这里的#代表着protect
- GeometricObject.java
package com.atshangguigu.Exer1; public class GeometricObject {//几何图形 //1.属性 protected String color; protected double weight; //2.get与set的使用 public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } //3.构造器 public GeometricObject(String color, double weight) { super(); this.color = color; this.weight = weight; } //进行方法的设定 public double findArea()//这里的面积写不出来,只能用微积分 { return 0.0; } }
- Circle.java
package com.atshangguigu.Exer1; public class Circle extends GeometricObject{ //注意刚开始是报错的,因为前面的父类之中的构造器是空参数的,所以这里我们是需要引入的. //1.属性 private double radius; //2.构造器 public Circle(double radius,String color, double weight) { super(color, weight); this.radius = radius; } //3.set与get的方法 public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } //4.方法 public double findArea() { return Math.PI*(this.radius)*(this.radius); } }
- MyRectangle.java
package com.atshangguigu.Exer1; public class MyRectangle extends GeometricObject{ //1.属性 private double height; private double weigh; //2.构造器 public MyRectangle(double height,double weigh,String color, double weight) { super(color, weight); this.height = height; this.weight = weigh; } //3.set与get的构建 public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getWeigh() { return weigh; } public void setWeigh(double weigh) { this.weight = weigh; } //4.进行求取面积 public double findArea() { return this.weight*this.height; } }
- GeometricTest.java
package com.atshangguigu.Exer1; public class GeometricTest { public static void main(String[] args) { //这个是测试两个圆形 GeometricTest test = new GeometricTest(); Circle c1 = new Circle(4.0, "黑色", 12.4); test.displayGeometricObject(c1); Circle c2 = new Circle(4.0, "白色", 12.4); test.displayGeometricObject(c2); boolean is = test.equalsArea(c1,c2); System.out.println("c1 和 c2的面积是否相等" + is); } //测试两个面积是否相等 public boolean equalsArea(GeometricObject o1,GeometricObject o2) { return o1.findArea() == o2.findArea(); } //显示对象的面积 public void displayGeometricObject(GeometricObject o) { System.out.println("面积是:" + o.findArea()); } }45.多态性练习:重写方法
- 答案是:运行时行为
package com.atshangguigu.Exer; //考查多态的笔试题目: public class InterviewTest1 { public static void main(String[] args) { base1 base = new Sub1();//多态了一下 base.add(1, 2, 3);//这里是调用是Sub1之中的. //为什么?因为可以看到代码是画出黄线的,因此是重写,并且从运行结果上看,也是重写. Sub1 s = (Sub1)base; s.add(1,2,3); //优先调用确定个的 } } class base1 { public void add(int a, int... arr) { System.out.println("base1"); } } class Sub1 extends base1 { public void add(int a, int[] arr) { System.out.println("sub_1"); } public void add(int a, int b, int c) { System.out.println("sub_2"); } }46.Object类结构的解析
//这个视频我没看呀,后面再看
47.==运算符的回顾 48.equals()的使用 49.重写equals()- 手动实现重写-----自动生成和之前的set与get是一样的
//对equals()进行重写 //重写的原则:比较两个对象的实体内容是否相同的(没有什么思路,那么看看String是如何写的) @Override public boolean equals(Object obj) { System.out.println("到底执行了吗?"); if(this == obj) { return true; } if(obj instanceof Customer) { Customer cus = (Customer)obj; //比较两个对象的属性是否是一样的 //另外的一种方法是 return this.age == cus.age && this.name.equals(cus.name); } return false; }50.总结equals()与==
- 不能写null.equals(x);
- "=="符号必须表示两边的类型相同或者一致.
package com.atshangguigu.Exer2; public class OrderTest { public static void main(String[] args) { Order order1 = new Order(1001,"AA"); Order order2 = new Order(1000,"BB"); System.out.println(order1.equals(order2)); Order order3 = new Order(1000,"BB"); System.out.println(order2.equals(order3));//true用==,注意,一般String是不能够直接用new的 String s1 = "CC"; String s2 = "CC"; System.out.println(s1 == s2);//true } } class Order { //属性 private int orderId; private String orderName; //set与get public int getOrderId() { return orderId; } public void setOrderId(int orderId) { this.orderId = orderId; } public String getOrderName() { return orderName; } public void setOrderName(String orderName) { this.orderName = orderName; } //构造器 public Order(int orderId, String orderName) { super(); this.orderId = orderId; this.orderName = orderName; } @Override public boolean equals(Object obj) { if(this == obj) {return true; } if(obj instanceof Order) { //向下转型 Order order = (Order)obj; //return this.orderId == order.orderId && this.orderName.equals(order.orderName); return this.orderId == order.orderId && this.orderName==order.orderName; } return false; } }52.equals()练习2:代码实现
package com.atshangguigu.Exer2; public class MyDateTest { public static void main(String[] args) { MyDate m1 = new MyDate(14, 3, 1976); MyDate m2 = new MyDate(14, 3, 1976); if (m1 == m2) { System.out.println("m1==m2"); } else { System.out.println("m1!=m2"); // m1 != m2 } if (m1.equals(m2)) { System.out.println("m1 is equal to m2");// m1 is equal to m2 } else { System.out.println("m1 is not equal to m2"); } } } class MyDate { //属性 private int day; private int month; private int year; //get和set public int getDay() { return day; } public void setDay(int day) { this.day = day; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } //构造器 public MyDate(int day, int month, int year) { super(); this.day = day; this.month = month; this.year = year; } //重写 public boolean equals(Object obj) { if(this == obj) { return true; } if(obj instanceof Object ) { MyDate mydate = (MyDate)obj; return this.day == mydate.day && this.month == mydate.month && this.year == mydate.year; } return false; } }53.toString()的使用
- 在Customer.java之中进行重写
public String toString() { return "Customer[name = "+ name + ",age = " + age + "]"; }
- toString.java之中进行继续写代码
package com.atshangguigu.java1; import java.util.Date; public class toString { public static void main(String[] args) { Customer cust = new Customer("Tom",12); System.out.println(cust.toString());//输出一个地址值com.atshangguigu.java1.Customer@15db9742 System.out.println(cust); String str = new String("MM"); System.out.println(str);//MM 这里的String是重写过的,因此是可以输出MM的 Date date = new Date(123456789L); System.out.println(date);//Fri Jan 02 18:17:36 CST 1970 } }54.Object类综合练习使用
- GeomtricObject.java
package com.atshangguigu.Exer3; public class GeometricObject { //1.属性 protected String color; protected double weight; //2.这里是一个构造器 public GeometricObject() { super(); this.color = "white"; this.weight = 1.0; } public GeometricObject(String color, double weight) { super(); this.color = color; this.weight = weight; } //3.get与set的设定 public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } }
- Circle.java
package com.atshangguigu.Exer3; public class Circle extends GeometricObject { //1.属性 private double radius; //2.构造器 public Circle() { super(); // this.color = "white"; //this.weight = 1.0; this.radius =1.0; } public Circle(double radius) { super(); this.color = "white"; this.weight = 1.0; this.radius = radius; } public Circle(String color, double weight, double radius) { super(color, weight); this.radius = radius; } //3.set与get public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } //4.方法 public double findArea()//计算圆的面积 { return Math.PI*radius*radius; } //5.重写 @Override public boolean equals(Object obj) { if(this == obj) { return true; } if(obj instanceof Object) { Circle circle = (Circle)(obj); return this.radius == radius; } return false; } @Override public String toString() { return "Circle [radius=" + radius + "]"; } }
- CircleTest.java
package com.atshangguigu.Exer3; public class CircleTest { public static void main(String[] args) { Circle c1 = new Circle("black",12 , 119); Circle c2 = new Circle("black",12 , 119); boolean isColor = c1.color.equals(c2.color);//这里是protected的,因此这里是可以这样写的,跟视频不一样 System.out.println("颜色是否相同" + isColor); boolean isRadius = c1.equals(c2); System.out.println("半径是否相同" + isRadius); System.out.println(c1.toString()); System.out.println(c2.toString()); } }55.单元测试方法的使用
- 想要测试哪个地方的代码就单独测试哪个地方的代码
package com.atshangguigu.java2; import org.junit.Test; public class JUnitTest { int num = 10; @Test public void testEquals() { String s1 = "MM"; String s2 = "GG"; System.out.println(s1.equals(s2)); System.out.println(num); show(); } public void show() { num = 20; System.out.println("show()........"); } @Test public void testToString() { String s2 = "MM"; System.out.println(s2); } }
- 在开发的过程之中,直接@Test,然后点一下它就行
- 基本数据类型真的很单薄.
- 基本数据类型和Object没有什么关系,基本数据类型相当于一个残疾人,把它给治好了,Interger之中需要一个int,体现了面向对象的思想,面向对象的功能.---这里就可以解释多态的过程之中哪个基本数据类型不能够被多态.(圣斗士---穿上射手座的圣衣就变的很牛逼啦)
- 基本数据类型 包装类和String类之间的转换
package com.atshangguigu.java2; import org.junit.Test; public class WrapperTest { //基本数据类型如何转换为包装类 :调用包装类的构造器 @Test public void test1() { int num1 = 10; Integer in1 = new Integer(num1); System.out.println(in1.toString()); Integer in2 = new Integer("123");//这里应当注意""之中应当是一个纯粹的数 System.out.println(in2); Float f1 = new Float(12.3f); Float f2 = new Float("12.3"); System.out.println(f1); System.out.println(f2); Boolean b1 = new Boolean(true); Boolean b2 = new Boolean("true"); Boolean b3 = new Boolean("true1234"); System.out.println(b2); System.out.println(b3); Order order = new Order(); System.out.println(order.isMale);//false System.out.println(order.isFemale);//null 此刻他是一个类了,所以是一个null } } class Order { boolean isMale; Boolean isFemale; }58.包装类转换为基本数据类型
//2.包装类转换为基本数据类型:调用包装类的xxxValue() @Test public void test2() { Integer in1 = new Integer(12); int i1 = in1.intValue(); System.out.println(i1 + 1); Float f1 = new Float(12.3); float f2 = f1.floatValue(); System.out.println(f2 + 1); }59.新特性:自动装箱与自动拆箱
@Test public void test3() { // int num1 = 10; // //基本数据类型---包装类的对象 // method(num1); //自动装箱: int num2 = 10; Integer in1 = num2;//这里用不着我们再去new //自动装箱 boolean b1 = true; Boolean b2 = b1; //自动拆箱:包装类转换为基本数据类型 System.out.println(in1.toString()); int num3 = in1; } public void method(Object obj) { System.out.println(obj); }
- 这个里面隐含着一个知识点:自动装箱与自动拆箱,公司里面用的都是jdk5.0版本以后的
//3.2String类型如何转换为基本数据类型或者包装类:调用包装类的parseXxx() @Test public void test5() { String str1 = "456";//这里是不能够写成456Yang,确保数字是可以转换的 //错误的情况 //int num1 = (int)str1; //Integer in1 = (Integer)str1; int num2 = Integer.parseInt(str1); System.out.println(num2 + 23); String str2 = "false";//只要不是true,都是false boolean b1 = Boolean.parseBoolean(str2); System.out.println(b1); } //3.1基本数据类型是如何转化为String类型:调用String重载的Valueof(Xxx xxx) @Test public void test4() { //方式一:连接运算 int num1 = 10; String str1 = num1 + "";//基本数据类型 + ""(字符串) //方式二:调用String的valueOf()的方式 float f1 = 12.3f; String str2 = String.valueOf(f1);//"12.3" System.out.println(str2); Double d1 = new Double(12.4); String str3 = String.valueOf(d1);//"12.4" System.out.println(str3); }61.包装类常见面试问题
package com.atshangguigu.java2; import org.junit.Test; public class Interview { @Test//先要导包:ctrl + shift + o public void test1() { Object o1 = true ? new Integer(1) : new Double(2.0); //上面的式子,有一个类型的提升,因此是Integer(1)变成了Double()类型的 System.out.println(o1);//1.0 } @Test public void test2() { Object o2; if (true) o2 = new Integer(1); else o2 = new Double(2.0); //上面没有进行结果的统一 System.out.println(o2);//1 } @Test public void method1() { Integer i = new Integer(1); Integer j = new Integer(1); System.out.println(i == j);//false ==的是地址 Integer m = 1; Integer n = 1; System.out.println(m == n);//true Integer x = 128; Integer y = 128; System.out.println(x == y);//false } //上面的这个题是有难度的--隐含知识点 //造好的数组,存了很多的数-128 - 127,这里的数组造好的原因是方便使用,但是当超过了127,就会new一个128 //上面也就是new了两个128 }62.包装类的课后练习
差一点,明天写
63.每日一考欢迎分享,转载请注明来源:内存溢出
评论列表(0条)