1 综合案例
Demo1
设计一个父类Shape(图形类),抽象类
常量:public static final double PI = 3.14;
抽象方法:void show():输出每一个属性值
void showArea():输出面积
void showPerimeter():输出周长
编写子类Circle(圆),继承Shape
属性:半径(double r) 构造方法:给属性赋值
方法:重写所有的抽象方法
圆面积:PI * r * r 圆周长:2 * PI * r
编写子类Rect(长方形),继承Shape
属性:长,宽 构造方法:给属性赋值
方法:重写所有的抽象方法
编写子类Square(正方形),继承Shape
属性:边长(int length) 构造方法:给属性赋值
方法:重写所有的抽象方法
在main方法,分别创建对象,并调用其方法。
public class Demo1 { public static void main(String[] args) { Shape circle = new Circle(10.0); circle.show(); circle.showArea(); circle.showPerimeter(); Rect rect= new Rect(100,80); rect.show(); rect.showArea(); rect.showPerimeter(); Square squ = new Square(100); squ.show(); squ.showArea(); squ.showPerimeter(); } } /* 设计一个父类Shape(图形类),抽象类 常量:public static final double PI = 3.14; 抽象方法:void show():输出每一个属性值 void showArea():输出面积 void showPerimeter():输出周长 编写子类Circle(圆),继承Shape 属性:半径(double r) 构造方法:给属性赋值 方法:重写所有的抽象方法 圆面积:PI * r * r 圆周长:2 * PI * r 编写子类Rect(长方形),继承Shape 属性:长,宽 构造方法:给属性赋值 方法:重写所有的抽象方法 编写子类Square(正方形),继承Shape 属性:边长(int length) 构造方法:给属性赋值 方法:重写所有的抽象方法 在main方法,分别创建对象,并调用其方法。 */ abstract class Shape{ //常量 public static final double PI = 3.14; //抽象方法 abstract void show(); abstract void showArea(); abstract void showPerimeter(); } class Circle extends Shape{ //属性 double r; //构造方法 Circle(double r){ this.r = r; } //方法 @Override void show() { System.out.println("半径:"+r); } @Override void showArea() { double result = PI * r * r; System.out.println("圆的面积是"+result); } @Override void showPerimeter() { double result = 2 * PI * r; System.out.println("圆的周长是"+result); } } class Rect extends Shape{ //属性 int length; int width; //构造方法 Rect(int length,int width){ this.length = length; this.width = width; } @Override void show() { System.out.println("长:"+length); System.out.println("宽:"+width); } @Override void showArea() { int result = length * width; System.out.println("面积是"+result); } @Override void showPerimeter() { int result = 2*(length+width); System.out.println("周长是"+result); } } class Square extends Shape{ //属性 int length; //构造方法 Square(int length){ this.length = length; } //方法 @Override void show() { System.out.println("边长是:"+length); } @Override void showArea() { int result = length * length; System.out.println("面积是"+result); } @Override void showPerimeter() { int result = 4 * length; System.out.println("周长是"+result); } }
2 接口
接口是一种特殊的引用数据类型,设计的工具。
1)接口的组成:常量和抽象方法
2)格式1:
interface 接口名{
//常量
public static final 数据类型 常量名=数值;
//抽象方法:public
public abstract 返回值类型 方法名(参数列表);
}
eg:
interface Car{
常量:
public static final double MAX_PRICE = 2000000.1;
public static final double MIN_PRICE = 1000.1;
抽象方法:
public abstract void start();
public abstract void run();
public abstract void stop();
}
3) 简化接口格式:
interface 接口名{
常量:
数据类型 常量名 = 常量值;
抽象方法:
返回值类型 方法名(参数);
}
eg:
interface Car{
double MAX_PRICE = 2000000.1;
double MIN_PRICE = 1000.1;
void start();
void run();
void stop();
}
案例:Demo2
public class Demo2 { public static void main(String[] args) { // Car car = new Car(); //接口不是类,不能直接使用接口来创建对象 Bmw bmw = new Bmw("宝马","12345678","黑色", 20000.1,"普通版"); bmw.start(); bmw.run(); bmw.stop(); } } interface Car{ //常量 public static final double MAX_PRICE=20000000.1; double MIN_PRICE = 20000.1; //抽象方法 public abstract void start(); void run(); void stop(); } class Bmw implements Car{ //属性 String name; String no; String color; int speed = 0; double price; String type; //构造方法:name,no,color,price,type赋值 Bmw(String name,String no,String color, double price,String type){ this.name = name; this.no = no; this.color = color; this.price = price; this.type = type; } //方法 @Override public void run() { System.out.println(name+"在行驶"); } @Override public void start() { System.out.println(name+"启动了"); } @Override public void stop() { System.out.println(name+"刹车了"); } } public class Demo3 { public static void main(String[] args) { Midea midea = new Midea("美的","壁挂式", 2000.99,"蓝色",28); midea.show(); midea.hot(5); midea.cool(9); //接口名.常量名 System.out.println(Kongtiao.MAX_PRICE); System.out.println(Kongtiao.MIN_PRICE); //实现类名.常量名 System.out.println(Midea.MAX_PRICE); System.out.println(Midea.MIN_PRICE); //对象名.常量名 System.out.println(midea.MAX_PRICE); System.out.println(midea.MIN_PRICE); } } interface Kongtiao{ //常量 public static final double MAX_PRICE=200000.11; double MIN_PRICE = 2000.99; //抽象方法 void show(); public abstract void cool(int degree); public abstract void hot(int degree); } //设计Kongtiao接口 //常量:MAX_PRICE = 200000.11; // MIN_PRICE = 2000.99; //抽象方法:void show():输出每一个属性的值 //void hot(int degree):升高degree度 //void cool(int degree):降低degree度 class Midea implements Kongtiao{ //属性:名称,类型,价格,颜色,温度 String name; String type; double price; String color; int degree; //构造方法: Midea(String name,String type, double price,String color, int degree){ this.name = name; this.type = type; this.price = price; this.color = color; this.degree = degree; } @Override public void cool(int degree) { this.degree = this.degree - degree; System.out.println(name+"降低"+degree +"度以后,当前的温度是"+this.degree); } @Override public void hot(int degree) { this.degree = this.degree + degree; System.out.println(name+"升高"+degree +"度以后,当前的温度是"+this.degree); } @Override public void show() { System.out.println(name+","+type+ ","+price+","+color+","+degree); } }
4)接口不是类,不能使用接口直接来创建对象。
5)实现类:编写一个类来实现接口,该类称为实现类,实现类必须要重写接口中所有的抽象方法,可以创建该实现类的对象。
6)访问常量:
a)接口名.常量名
b)实现类名.常量名
c)对象名.常量名
7)一个接口可以继承多个接口,但是该接口的实现类必须重写所有接口中,所有的抽象方法。接口之间可以多继承。
案例:Demo4
public class Demo4 { public static void main(String[] args) { Foo foo = new Foo(); foo.f1(); foo.f2(); foo.f3(); foo.f4(); } } interface Foo1{ void f1(); } interface Foo2{ void f2(); } interface Foo3{ void f3(); } interface Foo4 extends Foo1,Foo2,Foo3{ //f1(),f2(),f3() void f4(); } class Foo implements Foo4{ @Override public void f4() { System.out.println("接口Foo4中方法"); } @Override public void f1() { System.out.println("接口Foo1中方法"); } @Override public void f2() { System.out.println("接口Foo2中方法"); } @Override public void f3() { System.out.println("接口Foo3中方法"); } }
8)一个实现类可以实现多个接口,但是必须重写所有接口中所有的抽象方法。
案例:Demo5
public class Demo5 { public static void main(String[] args) { Koo koo = new Koo(); koo.f1(); koo.f2(); koo.f3(); koo.f4(); } } interface Koo1{ void f1(); } interface Koo2{ void f2(); } interface Koo3{ void f3(); } interface Koo4{ void f4(); } class Koo implements Koo1,Koo2,Koo3,Koo4{ @Override public void f1() { System.out.println("接口Koo1中方法"); } @Override public void f2() { System.out.println("接口Koo2中方法"); } @Override public void f3() { System.out.println("接口Koo3中方法"); } @Override public void f4() { System.out.println("接口Koo4中方法"); } }
9)在接口中也可以使用父类声明指向子类对象。
接口名 对象名 = new 实现类构造方法
该类对象只能访问接口中声明(定义)方法。
案例:Demo6
public class Demo6 { public static void main(String[] args) { //接口名 对象名 = new 实现类构造方法 Eoo1 eoo = new Eoo(); eoo.f1(); eoo.f2(); //该类对象可以访问接口中定义的方法。 } } interface Eoo1{ void f1(); void f2(); } class Eoo implements Eoo1{ @Override public void f1() { System.out.println("接口Eoo1中方法f1"); } @Override public void f2() { System.out.println("接口Eoo1中方法f2"); } }
该类对象不能访问实现类单独编写的方法(不是重写接口中的方法)。
案例:Demo7
public class Demo7 { public static void main(String[] args) { Zoo1 zoo = new Zoo(); zoo.f1(); // zoo.f2(); //该类对象只能访问接口中定义的方法,不能访问实现类 //单独编写的方法。 } } interface Zoo1{ void f1(); } class Zoo implements Zoo1{ @Override public void f1() { System.out.println("接口Zoo1中方法"); } public void f2(){ System.out.println("单独编写方法f2"); } }
10)接口跟抽象类之间区别
a)接口是一个特殊引用数据类型,主要用于设计。抽象类本质也是类,内部含有抽象方法而已。
b)接口由常量和抽象方法组成。抽象类可以由属性,构造方法,非抽象方法以及抽象方法组成。
c)接口之间可以多个继承,一个接口可以继承多个接口,但是该接口的实现类,必须要重写所有接口中所有的抽象方法(多继承);一个实现类可以实现多个接口,但是必须重写所有接口中所有的抽象方法。抽象类本质也是一个类,永远是单继承。
3 访问控制修饰符
1)public :公共的,可以用来修饰类,属性,构造方法以及方法,被public修饰的类,属性,构造方法以及方法,可以任意进行访问。
2)private:私有的,可以用来修饰属性,构造方法以及方法,被private修饰的属性,构造方法以及方法,只能在本类的内部访问,外界无法访问。
3)默认:系统默认提供的访问控制修饰符,可以用来修饰类,属性,构造方法以及方法,被默认修饰的类,属性,构造方法以及方法,只能在本包内访问,不同包之间无法访问。
4)protected:受保护的,可以用修饰属性,构造方法以及方法,被protected修饰属性,构造方法以及方法,在本包内任意访问,不同包之间,子类可以访问父类中使用protected所修饰的部分。
案例:Demo8~Demo9
public class Demo8 { public static void main(String[] args) { User user = new User(); System.out.println(user.name); //被public修饰的属性外界可以任意的访问 // System.out.println(user.age); //被private修饰的属性只能在本类的内部访问,外界 //无法访问 System.out.println(user.job); //被默认修饰的属性本包内任意访问。 System.out.println(user.salary); //被protected修饰的属性本包内任意访问。 } }
4 Object
Object是Java中最根上的类,所有的类都是直接或者间接的继承Object
案例:Demo10
public class Demo10 { public static void main(String[] args) { Moo moo = new Moo(); int num1 = moo.hashCode(); System.out.println(num1); Moo moo2 = new Moo(); int num2 = moo2.hashCode(); System.out.println(num2); } } class Moo{//默认直接继承Object }
hashCode():返回是一个int类型数字。
toString(): 返回当前对象的信息,返回结果是String类型的。默认返回值包名.类名@16进制数字。经常对toString()进行方法重写,返回属性信息。
案例:Demo11
public class Demo11 { public static void main(String[] args) { Person p1 = new Person("刘邦",36, 200000.99,"长安","13555555555"); String str = p1.toString(); //包名.类名@16进制数字 System.out.println(str); //com.tarena.demo.Person@4f1d0d } } class Person{ //属性: String name; int age; double salary; String address; String phone; //构造方法:给每一个属性赋值 Person(String name,int age, double salary,String address, String phone){ this.name = name; this.age = age; this.salary = salary; this.address = address; this.phone = phone; } @Override public String toString() { String str = "该Person对象的name="+ name+",age="+age+",salary="+salary +",address="+address+",phone="+ phone; return str; } }
5 super
1)super(参数):在子类构造方法第1行使用,调用父类中的构造方法。
2)super.属性名:访问被子类属性所覆盖的父类的属性。
案例:Demo12
public class Demo12 { public static void main(String[] args) { Noo2 noo = new Noo2(); System.out.println(noo.name); noo.showSuperName(); } } class Noo1{ String name = "刘彻"; } class Noo2 extends Noo1{ String name = "刘病已"; //super.属性名:访问被覆盖的父类的属性 void showSuperName(){ System.out.println(super.name); } }
3)super.方法名(参数):访问被子类重写的父类中的方法。
案例:Demo13
public class Demo13 { public static void main(String[] args) { Coo2 coo = new Coo2(); coo.f1(); coo.showSuperF1(); } } class Coo1{ void f1(){ System.out.println("父类Coo1中编写f1()"); } } class Coo2 extends Coo1{ void f1(){ System.out.println("子类Coo2重写父类" + "Coo1中的方法f1()"); } //super.方法名(参数):访问被子类重写的父类中的方法 void showSuperF1(){ super.f1(); } }