多态性
package com. atguigu. java;
public class InstanceofTest {
}
package com. atguigu. exer;
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. exer;
public class InstanceTest {
public static void main ( String[ ] args) {
InstanceTest test = new InstanceTest ( ) ;
test. method ( new Person ( ) ) ;
}
public void method ( Person e) {
String info = e. getInfo ( ) ;
System. out. println ( info) ;
if ( e instanceof Graduate ) {
System. out. println ( "Graduate" ) ;
} else if ( e instanceof Student ) {
System. out. println ( "Student" ) ;
} else {
System. out. println ( "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 + "\n" + "age:" + age + "\nschool:" + school;
}
}
class Graduate extends Student {
public String major = "IT" ;
public String getInfo ( ) {
return "Name:" + name + "\nage:" + age + "\nschool:" + school + "\nmajor" + major;
}
}
package com. atguigu. exer;
public class Geometric {
protected String color;
protected double weight;
public Geometric ( String color, double weight) {
super ( ) ;
this . color = color;
this . weight = weight;
}
public String getColor ( ) {
return this . color;
}
public void setColor ( String color) {
this . color = color;
}
public double getWeight ( ) {
return this . weight;
}
public void setWeight ( double weight) {
this . weight = weight;
}
public double findArea ( ) {
return 0.0 ;
}
}
package com. atguigu. exer;
public class Circle extends Geometric {
private double radius;
public Circle ( String color, double weight, double radius) {
super ( color, weight) ;
this . radius = radius;
}
public double getRadius ( ) {
return this . radius;
}
public void setRadius ( double radius) {
this . radius = radius;
}
public double findArea ( ) {
return Math. PI * this . radius * this . radius;
}
}
package com. atguigu. exer;
public class MyRectangle extends Geometric {
private double width;
private double height;
public MyRectangle ( String color, double weight, double width, double height) {
super ( color, weight) ;
this . width = width;
this . height = height;
}
public double getWidth ( ) {
return this . width;
}
public void setWidth ( double width) {
this . width = width;
}
public double getHeight ( ) {
return this . height;
}
public void setHeight ( double height) {
this . height = height;
}
public double findArea ( ) {
return this . width * this . height;
}
}
package com. atguigu. exer;
public class GeometricTest {
public static void main ( String[ ] args) {
GeometricTest test = new GeometricTest ( ) ;
Circle c1 = new Circle ( "red" , 1.0 , 2.3 ) ;
test. displayGeometric ( c1) ;
Circle c2 = new Circle ( "red" , 1.0 , 3.3 ) ;
System. out. println ( test. equalsArea ( c1, c2) ) ;
}
public boolean equalsArea ( Geometric o1, Geometric o2) {
return o1. findArea ( ) == o2. findArea ( ) ;
}
public void displayGeometric ( Geometric o) {
System. out. println ( o. findArea ( ) ) ;
}
}
package com. atguigu. java;
public class ObjectTest {
}
package com. atguigu. java;
public class EqualsTest {
String name;
int age;
public boolean equals ( Object obj) {
if ( this == obj) {
return true ;
}
if ( obj instanceof EqualsTest ) {
EqualsTest cust = ( EqualsTest) obj;
return ( this . age == cust. age && this . name. equals ( ( ( EqualsTest) obj) . name) ) ;
}
return false ;
}
}
package com. atguigu. exer1;
public class OrderTest {
public static void main ( String[ ] args) {
Order order1 = new Order ( 1001 , "AA" ) ;
Order order2 = new Order ( 1001 , "BB" ) ;
System. out. println ( order1. equals ( order2) ) ;
Order order3 = new Order ( 1001 , "BB" ) ;
System. out. println ( order2. equals ( order3) ) ;
}
}
class Order {
private int orderId;
private String orderName;
public void setOrderId ( int orderId) {
this . orderId = orderId;
}
public int getOrderId ( ) {
return this . orderId;
}
public void setOrderName ( String orderName) {
this . orderName = orderName;
}
public String getOrderName ( ) {
return this . orderName;
}
public Order ( int orderId, String orderName) {
this . orderId = orderId;
this . orderName = orderName;
}
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. exer;
public class GeometricObject {
protected String color;
protected 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. exer;
public class Circle extends GeometricObject {
private double radius;
public Circle ( ) {
super ( ) ;
this . 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.14 * radius* radius;
}
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 ;
}
public String toString ( ) {
return "Circle [radius = " + radius+ "]" ;
}
}
package com. atguigu. exer;
public class CircleTest {
public static void main ( String[ ] args) {
Circle circle1 = new Circle ( 2.3 ) ;
Circle circle2 = new Circle ( 2.3 , "white" , 2.0 ) ;
System. out. println ( circle1. getColor ( ) . equals ( circle2. color) ) ;
System. out. println ( circle1. equals ( circle2) ) ;
System. out. println ( circle1) ;
}
}
package com. atguigu. java;
public class JUnitTest {
}
package com. atguigu. java;
public class WrapperTest {
public static void main ( String[ ] args) {
int num1 = 10 ;
Integer in1 = new Integer ( num1) ;
System. out. println ( in1. toString ( ) ) ;
Integer in2 = new Integer ( "123" ) ;
System. out. println ( in2. toString ( ) ) ;
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 ( "true123" ) ;
System. out. println ( b2) ;
Order order = new Order ( ) ;
System. out. println ( order. isMale) ;
System. out. println ( order. isFemale) ;
int i1 = in1. intValue ( ) ;
System. out. println ( i1) ;
float f = f1. floatValue ( ) ;
System. out. println ( f1) ;
Integer int1 = 10 ;
Boolean b3 = true ;
System. out. println ( int1) ;
int num2 = 10 ;
String str1 = num1 + "" ;
float f4 = 12.3f ;
String str2 = String. valueOf ( f4) ;
Double d1 = new Double ( 12.4 ) ;
String str3 = String. valueOf ( d1) ;
System. out. println ( str3) ;
String str4 = "123" ;
int num5 = Integer. parseInt ( str4) ;
System. out. print ( num5) ;
}
}
class Order {
boolean isMale;
Boolean isFemale;
}
package com. atguigu. java;
public class InterviewTest {
public static void main ( String[ ] args) {
Integer i = new Integer ( 1 ) ;
Integer j = new Integer ( 1 ) ;
System. out. println ( i == j) ;
Integer m = 1 ;
Integer n = 1 ;
System. out. println ( m == n) ;
Integer x = 128 ;
Integer y = 128 ;
System. out. println ( x == y) ;
}
}
package com. atguigu. exer;
import java. util. Scanner;
import java. util. Vector;
public class ScoreTest {
public static void main ( String[ ] args) {
Scanner scan = new Scanner ( System. in) ;
Vector v = new Vector ( ) ;
int maxScore = 0 ;
for ( ; ; ) {
System. out. println ( "请输入学生成绩(以负数代表输入结束)" ) ;
int score = scan. nextInt ( ) ;
if ( score < 0 ) {
break ;
}
if ( score > 100 ) {
System. out. println ( "输入的数据非法,请重新输入" ) ;
continue ;
}
v. addElement ( score) ;
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;
if ( maxScore - score <= 10 ) {
level = 'A' ;
} else if ( maxScore - score <= 20 ) {
level = 'B' ;
} else if ( maxScore - score <= 30 ) {
level = 'C' ;
} else {
level = 'D' ;
}
System. out. println ( i + "" + score + level) ;
}
}
}