面向对象有3大特征:封装,继承,多态
继承:父子概念的继承:圆继承于图形
圆是子概念(子类型Sub class)图形是父类型(Super Class)
继承在语法方面的好处:子类共享了父类的属性和方法的定义,子类复用了父类的属性和方法。节省了代码。
继承体现了多态:父类型变量可以引用各种各样的子类型实例
个体的多态:父类型的子类型实例是多种多样的
行为的多态:父类型定义方法被子类重写为多种多样的,重载也是多态的方法。
package day2;
/*
* 图形 类型 其中x,y代表图形的位置
* 图形作为父类型,被圆和矩形继承
*/
public class Shape {
int x;
int y;
//图形向上移动的方法
public void up(int dy){
y -= dy;
}
public void up(){
y -- ;
}
/*检查当前图形是否包含(contains)坐标(x,y)*/
public boolean contains(int x,int y){
return false;
}
}
package day2;
/*
* 圆继承了图形,表示圆是一个图形。
* 圆会自动继承父类型的属性和方法
* 其中:Shape称为父类型(super class)
* Circle称为子类型(Sub class)
*/
public class Circle extends Shape{
int r;
public Circle(int x,int y,int r){
this.x = x;//this.x从父类继承的属性
this.y = y;
this.r = r;
}
//计算当前圆对象的面积
public double area(){
return 3.14 * this.r *this.r;
}
/*重写(覆盖)了父类的方法*/
public boolean contains(int x,int y){
int a = this.x-x;
int b = this.y -y;
double c = Math.sqrt(a*a+b*b);
return c<=r;
}
}
package day2;
/*
* Demo05与Shape 和Circle 在同一个包中
* Circle可以从Shape中继承属性和方法
* x属性和up方法定义在Shape类中,被子类Cirle继承了。
*/
public class Demo05 {
public static void main(String[] args) {
Circle c = new Circle(3,4,5);
c.up();//Circle从Shape中继承了方法up
System.out.println(c.x);//Circle继承了Shape中的x
System.out.println(c.y);
}
}
package day2;
/*
* 子类型对象可以赋值给父类型变量
* 变量是代词,父类型代词可以引用子类型东西
* 变量的类型定义的属性和方法才能访问!
* 如:Shape s;s只能访问Shape上声明的属性和方法
*/
public class Demo06 {
public static void main(String[] args){
Circle c = new Circle(3,4,5);
Shape s = c;//父类型变量s引用了子类型实例
//s和c引用了同一个对象new Circle(3,4,5)
s.up();
System.out.println(c.r);
System.out.println(c.area());
// System.out.println(s.area());//编译错误
// System.out.println(s.r);//在Shape上没有定义r属性!
}
}
package day2;
/*
* 1)构造器是不能继承的!
* 2)子类型构造器默认调用父类型的无参数构造器
*/
public class Demo07 {
public static void main(String[] args) {
// Goo goo = new Goo(5);//编译错误,Goo中没有Goo(int)构造器
Goo goo = new Goo();//调用的是子类Goo的默认构造器,
//不是父类型的Soo()构造器
Moo moo = new Moo();
}
}
class Noo{
public Noo(){System.out.println("Call Noo()");}
}
class Moo extends Noo{
public Moo(){}
}
class Soo{
public Soo(){}
public Soo(int a){
System.out.println("Call Soo(int)");
}
}
class Goo extends Soo{}
package day2;
/*
* 1)类一定有构造器
* 2)子类一定调用父类构造器
* 2.1)子类构造器默认调用父类无参数构造器
* 2.2)如果父类没有无参数构造器,必须使用super()调用父类构造器
*关于super():一定在子类构造器第一行使用!如果没有默认存在super()!
*是java默认添加的super();
*/
public class Demo08 {
public static void main(String[] args){
}
}
class Xoo{
public Xoo(int s){
System.out.println("Call Xoo(int)");
}
}
//super()用于在子类构造器中调用父类的构造器
class Yoo extends Xoo{
// public Yoo() {}//编译错误,子类调用不到父类型无参数构造器
public Yoo(){
// super();//编译错误,子类调用不到父类型无参数构造器
super(100);//super(100)调用了父类Xoo(int)构造器
}
}
package day2;
/*
* super()的语法
*/
public class Demo09 {
public static void main(String[] args){
}
}
class Aoo{
}
class Boo extends Aoo{
public Boo(){
// System.out.println("hi");
super();//super()必须用在子类构造器的第一行!
}
public Boo(int a){
//super();//默认在构造器的第一行存在super()
}
}
package day2;
/*
* this()
* 调用本类的其他构造器
* 按照参数调用构造器,必须在构造器中使用,必须在第一行使用
* this()与super()互斥
* 区别:this.与this()
* this.是访问当前对象
* this()是调用本类构造器
*/
public class Demo10 {
public static void main(String[] args){
Cell c = new Cell();
System.out.println(c.x+","+c.y);
}
}
class Cell{
int x;int y;
public Cell(){
this(1,1);//调用本类的其他构造器
}
public Cell(int x,int y){
this.x = x;
this.y = y;
}
}
package day2;
/*
* 矩形
*/
public class Rect extends Shape {
int width;//宽
int height;//高
public Rect(int x,int y,int w,int h){
super.x = x;//super.访问从父类中继承的属性
this.y = y;//this.访问当前对象的属性,y继承与父类
this.height = h;
width = w;
}
public double area(){//计算矩形面积的方法
return width * height;
}
/*重写父类方法*/
public boolean contains(int x,int y){
int dx = x - this.x;//0<=dx<width
int dy = y - this.y;//0<=dy<height
return 0<=dx && dx<width && 0<=dy && dy<height;
}
}
package day2;
public class Demo11 {
public static void main(String[] args) {
Circle c = new Circle(3,4,5);
Rect r = new Rect(3,4,5,6);
Shape s = c;
Shape s1 = r;
//Circle x= s;//编译错误,父类型变量不能赋值给子类型
Circle x = (Circle)s;//正常执行
//Circle y = (Circle)s1;//运行异常,类型转换异常
//instanceof instance:实例 of :的
//instanceof 运算 检查变量引用的对象的类型是否兼容
//s引用的圆对象,s instanceof Circle 检查s引用的对象是否是Circle类型的实例!
System.out .println(s instanceof Circle);//true
System.out.println(s1 instanceof Circle);//false
test(c);
test(r);
}
public static void test(Shape s){//多态的参数
//if(s instanceof Circle)保护了(Circle)s不会出现异常
//实现了安全的类型转换
if(s instanceof Circle){
Circle c= (Circle) s;
System.out.println("这是一个圆,面积"+c.area());
}
if(s instanceof Rect){
Rect r = (Rect) s;
System.out.println("这是一个矩形,面积"+r.area());
}
}
}
package day2;
/*
* 重写方法的调用
*/
public class Demo12 {
public static void main(String[] args){
Shape s1 = new Circle(3,4,5);
//s1引用的是圆对象,调用重写方法时候
//执行的是子类圆重写的方法!并不执行Shape声明的方法
System.out.println(s1.contains(1, 1));
System.out.println(s1.contains(10, 10));
print(new Circle(5,5,4),new Rect(5,5,6,7));
print(new Circle(8,8,4),new Circle(5,5,4));
}
public static void print(Shape s1,Shape s2){
for(int y=0;y<20;y++){
for(int x=0; x<20;x++){
//重写的方法在运行期间动态绑定到对象的方法
if(s1.contains(x, y)&&s2.contains(x, y)){
System.out.print("+");
}else if(s1.contains(x, y)){
System.out.print("-");
}else if(s2.contains(x, y)){
System.out.print("|");
}else{
System.out.print(" ");
}
}
}
}
}