用abstract 修饰的类:
当我们父类的方法不需要方法体时,我们可以将我们的方法修饰成抽象的
抽象方法格式:
abstract 修饰符 返回值类型 方法名(参数列表);
抽象类的定义格式:
abstract class 类名{}
注意事项:
抽象类中可以没有抽象方法,有抽向方法的类一定是抽象类
抽象类不能创建对象,需要使用子类向上转型
3.抽象类本身无意义,所以我们应该使用一个类去继承它,然后重写里面所有的
抽象方法,或者,这个子类本身也得是一个抽象类
抽象类有构造方法,本身无意义,但是可以给子类对象初始化,因为继承关系中,子类对象想要创建成功,必须到父类构造中走一圈
abstract 不能和final共存,因为被final修饰的类不能被继承而abstract修饰的类必须被继承
如果想要使用抽象类中的对象,可以使用抽象类多态的形式
/** - 定义一个抽象类形状(Shape),包含两个方法,求周长和面积(抽象方法) 定义一个类长方形,实现抽象类中的方法 (长,宽) (构造方法) 定义一个类圆,实现抽象类中的方法 (半径) (构造方法) 在测试类中测试 创建一个长方形和圆,分别求周长和面积 创建一个方法,既可以求圆的周长面积,也可以求长方形的周长和面积 */ public class AbstractTest2 { public static void main(String[] args) { Shape r = new Rect(20,10); /*System.out.println(r.getPrimeter()); System.out.println(r.getArea());*/ Shape c = new Circle(4); /*System.out.println(c.getPrimeter()); System.out.println(c.getArea());*/ print(r); print(c); } public static void print(Shape s) { System.out.println("周长为:" + s.getPrimeter()); System.out.println("面积为: "+ s.getArea()); } } abstract class Shape{ abstract public double getPrimeter(); abstract public double getArea(); } class Rect extends Shape{ double length; double width; public Rect() {} public Rect(double length,double width) { this.length = length; this.width = width; } public double getPrimeter() { return 2*(length+width); } public double getArea() { return length*width; } } class Circle extends Shape{ public static final double PI = 3.14; double r; public Circle() {} public Circle(double r) { this.r = r; } public double getPrimeter() { return 2*PI*r; } public double getArea() { return PI*r*r; } }案例: 定义一个Person类,里面包含,姓名,年龄,性别和手机(品牌,型号,价格,颜色):
创建一个对象: 张三,18,男,手机(华为,p20,1888,“黑色”)
public class PersonTest { public static void main(String[] args) { Person p = new Person(); p.setName("张三"); p.setAge(18); p.setGender('男'); Mobile m = new Mobile(); m.setBrand("华为"); m.setType("p20"); m.setColor("黑色"); m.setPrice(1888); p.setMobile(m); p.show(); Person p2 = new Person("李四",29,'女',new Mobile("小米","note8","白色",1000)); p2.show(); } } /** - 继承: is a Teacher is a Person - 组合: has a Person has a Mobile - 聚合: has a - Person 和 Mobile 关系就叫做组合关系 */ class Person{ private String name; private int age; private char gender; private Mobile mobile;//组合 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 char getGender() { return gender; } public void setGender(char gender) { this.gender = gender; } public Mobile getMobile() { return mobile; } public void setMobile(Mobile mobile) { this.mobile = mobile; } public Person() { super(); } public Person(String name, int age, char gender, Mobile mobile) { super(); this.name = name; this.age = age; this.gender = gender; this.mobile = mobile; } public void show() { System.out.println("姓名:"+name+",年龄:"+age+",性别:"+gender+",品牌:"+mobile.getBrand()+",型号"+mobile.getType()+",颜色:"+mobile.getColor()+",价格:"+mobile.getPrice()); } } class Mobile{ private String brand; private String type; private String color; private double price; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Mobile() { super(); } public Mobile(String brand, String type, String color, double price) { super(); this.brand = brand; this.type = type; this.color = color; this.price = price; } } 接口interface : 叫做扩展功能
定义格式:
interface 接口名{}
注意事项:
1. 接口中只能定义常量,默认public static final修饰
2. 接口中只能定义抽象方法(1.8之前) 默认是public abstract 修饰
3. 接口不能创建对象,使用子类向上转型
4. 接口的子类: 实现了接口的类 class 子类名 implements 接口1,接口2{}
5. 接口的子类要么实现接口中所有的抽象方法要么自己是一个抽象类
6. 一个类可以实现多个接口,并且可以在继承类的同时实现多个接口
7. 接口中没有构造方法
8. jdk8之后接口中可以定义已经实现的方法,但是必须使用static/default 修饰
9. 接口不能实现接口,只能继承接口,并且可以多继承
class 子类名 implements 接口1,接口2{}
练习1:
定义一个接口,定义两个抽象方法,写一个类实现该接口,并实现里面的方法,使用接口多态创建对象,并调用方法
/** - . 定义一个接口InterA, 里面包含一个抽象方法,一个常量值a=100 - . 定义一个接口,InterB,继承接口InterA, 在写一个抽象方法 - . 定义个类Test, - . 定义一个类Demo 继承Test 并 实现 InterB,实现里面所有的抽象方法 - . 在测试类中,创建InterB 类型的变量,调用里面的方法 */ public class InterfaceTest { public static void main(String[] args) { InterB b = new Demo();// 接口的多态 b.test(); b.test2(); } } interface InterA{ void test(); int a = 100; } interface InterB extends InterA{ public void test2(); } class Test{ } class Demo extends Test implements InterB{ @Override public void test() { System.out.println("test"); } @Override public void test2() { System.out.println("test2"); } }练习2:
编写一个抽象类Animal,抽象类中包括属性:name(String类型),抽象方法:speak()。
编写一个宠物接口pet,接口中包括方法:eat()。
再编写一个类cat,实现该接口和抽象类中的所有方法。
speat(): “miao, my name is xxx”
eat(): “I want to eat some fish”
在main中进行测试,输出: 给猫赋值一个名字,调用speak,eat 方法
public class InterfaceTest2 { public static void main(String[] args) { Pet p = new Cat(); p.eat(); //p.speak();//speak 并不存在与Pet接口中,所有他是子类中独有的方法,在多态的时候,调不了 Animal a = new Cat("小花"); a.speak(); //a.eat();//不能调子中独有的 } } //the type animal is already defined: 代表本包中已经有该名字的类: 换一个名: 建一个包 abstract class Animal{ String name; abstract public void speak(); } interface Pet{ void eat(); } class Cat extends Animal implements Pet{ public Cat() {} public Cat(String name) { this.name = name; } @Override public void eat() { System.out.println("I want to eat some fish"); } @Override public void speak() { System.out.println("miao, my name is "+name); } }使用notepad++ 编写一个带包的HelloWorld
package com.doit.test; class HelloWorld{ public static void main(String[]args){ System.out.println("hello world"); } }使用命令行如何执行:
package com.doit.test;
手动式:
使用javac命令编译 javac 文件名.java javac HelloWorld.java
在类所在的文件夹下,按照包的格式把对应的文件夹创建出来,把.class文件拷贝到文件夹中
使用java 包名.类名 运行
自动式:
javac(空格) -d (空格).(空格) 文件名.javajava 包名.类名导包: import 全类名;
顺序: package import class
public: 修饰类, 修饰变量和方法 , 权限最大,可以在任意包内调用
protected: 修饰变量和方法 , 当前包内 随意访问 ,跨包只能在继承过父类的子类中使用
(default)/package: 修饰类,变量,方法 , 只能在当前包内访问
private: 修饰方法和变量 , 只能在本类中访问
protected: 修饰方法和变量
可以在不同包的子类中访问, 可以使用super关键字,创建子类的对象访问(创建父类的对象无法访问)
修饰外部类的修饰符:
public (default) final abstract