package com.atguigu.java3;
/*
* 面向对象特征之三:多态性
*
* 1、理解多态性:可以理解为一个事务的多种形态
* 2、何为多态性
* 对象的多态性:子类的对象赋给父类的引用
* 3、多态的使用:虚拟方法调用
* 有了对象的多态性以后,我们在编译期,只能调用父类声明的方法,但在运行期,
* 我们实际上执行的是子类重写父类的方法
*
* 4、多态性的使用前提 :①类的继承关系 ②方法的重写
*
* 5、对象的多态性,只适用于方法,不适用于属性
*
*/
public class PersonTest {
public static void main(String[] args) {
Person p1 = new Person();
p1.eat();
Man man = new Man();
man.eat();
man.age = 25;
man.earnMoney();
// ********************************************************
// 对象多态性:父类的引用指向子类的对象
Person p2 = new Man();
p2.eat();
p2.walk();
Man m1 = (Man) p2;
m1.earnMoney();
m1.isSomking = true;
// System.out.println(p2.id);
System.out.println("******************************");
/*
* instancrof关键字的使用 a instanceof A:判断对象a是否是类A的实例。如果是返回true 不是返回 false
*
*/
if (p2 instanceof Woman) {
Woman w1 = (Woman) p2;
w1.goShopping();
System.out.println("****women****");
}
}
// 编译时通过,运行时不通过
// Person p3 = new Woman();
// Man m3 = (Man)p3;
Object obj = new Woman();
Person p = (Person) obj;
}
package com.atguigu.exer;
/*
* 1.若子类重写了父类方法,就意味着子类里定义的方法彻底覆盖了夫雷里的同名方法
* 系统将不可能把父类里的方法转移到子类中
*
* 2.对于实例变量则不存在这样的现象,即使子类里定义了与父类完全相同的实例变量
* 这个实例变量依然不可能覆盖父类中定义的实例变量
*
*
*
*
*/
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);
}
}
public class FieldMethodTest {
public static void main(String[] args) {
Sub s = new Sub();
System.out.println(s.count);
s.display();
Base b = s;
System.out.println(b == s);
System.out.println(b.count);
b.display();
}
}
package com.atguigu.exer1;
public class GeometricObject {
private String color;
private double weght;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getWeght() {
return weght;
}
public void setWeght(double weght) {
this.weght = weght;
}
public GeometricObject(String color, double weght) {
super();
this.color = color;
this.weght = weght;
}
public double findArea() {
return 0.0;
}
}
package com.atguigu.exer1;
public class Circle extends GeometricObject {
private double radius;
public Circle(double radius, String color, double weght) {
super(color, weght);
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double findArea() {
return 3.14 * radius * radius;
}
}
package com.atguigu.exer1;
public class MyRectangle extends GeometricObject {
private double width;
private double heigth;
public MyRectangle(double width, double heigth, String color, double weght) {
super(color, weght);
// TODO Auto-generated constructor stub
this.width = width;
this.heigth = heigth;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeigth() {
return heigth;
}
public void setHeigth(double heigth) {
this.heigth = heigth;
}
public double findArea() {
return width * heigth;
}
}
package com.atguigu.exer1;
public class GeomertricTest {
public static void main(String[] args) {
GeomertricTest test = new GeomertricTest();
Circle c1 = new Circle(2.3, "white", 1.0);
test.displayGeomertricObject(c1);
Circle c2 = new Circle(3.3, "white", 1.0);
test.displayGeomertricObject(c2);
}
public void displayGeomertricObject(GeometricObject o) {
System.out.println("面积为" + o.findArea());
}
// 测试两个对象面级是否相等
public boolean equalsArea(GeometricObject o1, GeometricObject o2) {
return o1.findArea() == o2.findArea();
}
}
package com.atguigu.java1;
/*
* java.lang.Object类
* 1.Object类是所有Java类的根父类
* 2.如果在类的声明中未使用extends关键字指明其父类,则默认父类为java.lang.Object
* 3.Object类中的功能(属性、方法)就具有通用性
*
* 4.Object只有一个空参构造器
*
*/
public class ObjectTest {
}
package com.atguigu.java1;
/*
* 面试题 == 和equals()区别
*
* 一、回顾==的使用
* ==:运算符
* 1.可以使用在基本数据类型变量和引用数据类型变量中
* 2.如果比较的是基本数据类型变量:比较两个变量保存的数据是否相等
* 如果比较的是引用数据类型变量:比较两个对象的地址值是否相同,即两个引用是否指向同一个实体
*
*
* 二、equals()方法的 使用
* 1.是一个方法,而非运算符
* 2.只能适用于引用数据类型
* 3.Object类中equals()的定义
* public boolean equals(Object obj){
* return (this==obj);
* }
*
* 4.像String、date、file、包装类等都重写了Object类中equals()方法
* 重写以后比较的是实体内容是否相同
*
* 5.通常情况下,我们自定义的类如果使用equals()的话,也通常是比较两个对象的“实体内容”是否相同。
* 那我们就需要对Object类中的equals()进行重写
*重写的原则:比较两个对象的实体内容是否相同
*
*
*
*
*
*
*/
public class EqualsTest {
}
package com.atguigu.java1;
public class Customer {
private String name;
private int 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 Customer(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
}
package com.atguigu.exer2;
public class OrderTest {
public static void main(String[] args) {
Order order1 = new Order(1001, "AA");
Order order2 = new Order(1002, "BB");
System.out.println(order1.equals(order2));
}
}
class Order {
private int orderId;
private String orderName;
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 false;
}
}
package com.atguigu.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");
}
if (m1.equals(m2)) {
System.out.println("m1 is equa to m2");
} else {
System.out.println("mi is not equa tom2");
}
}
}
class MyDate {
private int day;
private int month;
private int year;
public MyDate(int day, int month, int year) {
super();
this.day = day;
this.month = month;
this.year = year;
}
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 boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof MyDate) {
MyDate myDate = (MyDate) obj;
return this.day == myDate.day && this.month == myDate.month && this.year == myDate.year;
}
return false;
}
// @Override
// public boolean equals(Object obj) {
// if (this == obj)
// return true;
// if (obj == null)
// return false;
// if (getClass() != obj.getClass())
// return false;
// MyDate other = (MyDate) obj;
// if (day != other.day)
// return false;
// if (month != other.month)
// return false;
// if (year != other.year)
// return false;
// return true;
// }
}
package com.atguigu.exer2;
/*
* Object类中同String()的使用
* 1.当我们输出一个对象的引用时,实际上就是调用当前对象的提String()
*
* 2.Object类中toString()的定义
* public String toString(){
* return getClass().getName() + "@" + Integer.toHexString(hashCode());
* }
*
* 3.像String、Date、File、包装类等都重写了Object类中toString()的方法。
* 使得在调用对象的toString()时,返回“实体内容”信息
* 4.自定义类也可以重写toString()方法,当调用此方法时,返回对象的”实体内容“
*
*
*
*
*
*/
import java.util.Date;
import com.atguigu.java1.Customer;
public class ToStringTest {
public static void main(String[] args) {
Customer cust1 = new Customer("tom", 21);
System.out.println(cust1.toString());
System.out.println(cust1);
String str = new String("MM");
System.out.println(str);
Date date = new Date(342534534534L);
System.out.println(date.toString());
}
}
package com.atguigu.exer3;
public class GeometricObject {
private String color;
private double weight;
public GeometricObject(){
super();
this.color = "white";
this.weight = 1.0;
}
public GeometricObject(String color, double weight) {
super();
this.color = color;
this.weight = weight;
}
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;
}
}
package com.atguigu.exer3;
public class Circle extends GeometricObject {
private double radius;
public Circle() {
super();
radius = 1.0;
}
public Circle(double radius) {
super();
this.radius = radius;
}
public Circle(double radius, String color, double weight) {
super(color, weight);
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double findArea() {
return 3.13 * radius * radius;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Circle) {
Circle c = (Circle) obj;
return this.radius == c.radius;
}
return false;
}
@Override
public String toString() {
return "Circle [radius=" + radius + "]";
}
}
package com.atguigu.exer3;
public class CircleTest {
public static void main(String[] args) {
Circle circle1 = new Circle(2.3);
Circle circle2 = new Circle(2.3, "while", 2.3);
System.out.println(circle1.getColor().equals(circle2.getColor()));
System.out.println(circle1.equals(circle2));
}
}
package com.atguigu.java2;
import org.junit.Test;
/*
* 包装类的使用:
* 1.java提供了8种基本数据类型对应的包装类,使得基本数简单变量具有类的特征
*
* 2.掌握的:基本数据类型、包装类、String三者之间的相互转换
*
*
*
*
*
*
*/
public class WrapperTest {
// String类型--->基本数据类型。调用包装的parsexxx
@Test
public void test5() {
String str1 = "123";
int num2 = Integer.parseInt(str1);
System.out.println(num2);
String str2 = "true";
boolean b1 = Boolean.parseBoolean(str2);
System.out.println(b1);
}
// 基本数据类型、包装类-->String类型,调用String重载的valueOf
@Test
public void test4() {
float f1 = 12.3f;
String str1 = String.valueOf(f1);
System.out.println(str1);
Double d1 = new Double(12.3);
String str2 = String.valueOf(d1);
System.out.println(str2);
}
// 自动装箱与自动拆箱
@Test
public void test3() {
Integer int1 = new Integer(23);
int i2 = int1;
System.out.println(i2);
}
// 包装类--->基本数据类型:调用包装类的xxxVlua
@Test
public void test2() {
Integer in = new Integer(12);
int i1 = in.intValue();
System.out.println(i1);
Float f1 = in.floatValue();
System.out.println(f1);
}
// 基本数据类型---> 包装类:调用包装类的构造器
@Test
public void test1() {
int num1 = 10;
Integer in1 = new Integer(num1);
System.out.println(in1.toString());
Integer in2 = new Integer("22213");
System.out.println(in2);
Float f1 = new Float(23.23);
System.out.println(f1);
}
}
package com.atguigu.exer4;
import java.util.Scanner;
import java.util.Vector;
public class ScoreTest {
public static void main(String[] args) {
// 1.实例化Scanner,从 键盘获取学生成绩
Scanner scan = new Scanner(System.in);
// 2.创建Vector对象:Vector v= new Vector(),相当于原来的数组
Vector v = new Vector();
// 3.通过for()或while方式。
int maxScore = 0;
for (;;) {
System.out.println("输入成绩(以负数代表输入结束)");
int score = scan.nextInt();
if (score < 0) {
break;
}
if (score > 100) {
System.out.println("输入数据非法");
continue;
}
// 4.给Vector中添加数组:V.addElement(Object obj)
// Integer inScore = new Integer(score);
v.addElement(score);
// 5.当输入是负数时,跳出循环
// 6.获取学生的最大值
if (maxScore < score) {
maxScore = score;
}
}
char level;
for (int i = 0; i < v.size(); i++) {
Object obj = v.elementAt(i);
Integer inScore = (Integer) obj;
int score = inScore.intValue();
if (maxScore - score <= 10) {
level = 'A';
}
if (maxScore - score <= 10) {
level = 'B';
}
if (maxScore - score <= 10) {
level = 'C';
}
if (maxScore - score <= 10) {
level = 'D';
}
System.out.println(i + score + "level");
}
}
}