包
权限修饰符
// 1.private 只能本类中访问
private void show1() {
System.out.println("private");
}
// 2.缺省:本类,同一个包下的类中。
void show2() {
System.out.println("缺省");
}
// 3.protected:本类,同一个包下的类中,其他包下的子类
protected void show3() {
System.out.println("protected");
}
// 4.任何地方都可以
public void show4() {
System.out.println("public");
}
final
常量
枚举
抽象类
抽象类、抽象方法概述
public abstract class Animal {
public abstract void run();
}
public class Tiger extends Animal{
@Override
public void run() {
System.out.println("老虎跑的快");
}
}
public class Test {
public static void main(String[] args) {
Tiger t = new Tiger();
t.run();
/**
* 结果:
老虎跑的快
*/
}
}
抽象类案例
/**
抽象父类
*/
public abstract class Card {
private String name;
private double money;
/**
子类一定要支付的,但每个子类支付的情况不一样,所以父类把支付
定义为抽象类方法,交给子类实现
*/
public abstract void pay(double money);
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
public class GoldCard extends Card{
@Override
public void pay(double money) {
//优惠后的金额
double rs = money * 0.8;
double lastMoney = getMoney() - rs;
System.out.println(getName() +"当前账户总金额为:" + getMoney() +
", 当前消费了:" + rs + ", 当前余额为:" + lastMoney);
setMoney(lastMoney); //更新账户余额
}
}
public class Test {
public static void main(String[] args) {
GoldCard c = new GoldCard();
c.setMoney(10000); //父类的
c.setName("张三");
c.pay(300);
}
}
抽象类特征、注意事项
抽象类应用知识:模板方法模式
接口
接口概述、特点
/**
接口
接口不能被创建对象
*/
public interface SportManInterface {
// 接口中的成员:JDK1.8之前只有常量和抽象方法
// 1. 常量
// public static final 可以不写,接口默认会加上
public static final String SCHOOL_NAME = "清华大学";
// 2. 抽象方法
// public abstract 可以不写,接口默认会加上
public abstract void run();
public abstract void eat();
}
接口的基本使用:被实现
public interface SportMan {
void run();
void competition();
}
public interface Law {
void rule();
}
/**
实现类
*/
public class PingPongMan implements Law, SportMan{
private String name;
public PingPongMan(String name) {
this.name = name;
}
@Override
public void rule() {
System.out.println(name + "要守法");
}
@Override
public void run() {
System.out.println(name + "要跑步");
}
@Override
public void competition() {
System.out.println(name + "要比赛");
}
}
public class Test {
public static void main(String[] args) {
PingPongMan p = new PingPongMan("张三");
p.run();
p.competition();
p.rule();
}
}
接口与接口的关系:多继承
JDK8 之后新增接口方法
public interface SportManInter {
/**
默认方法(实例方法)
接口不能创建对象,这个方法只能过继给实现类,由实现类的对象调用
*/
default void run() {
System.out.println("跑");
}
class PinPongMan implements SportManInter {}
class Test {
public static void main(String[] args) {
PinPongMan p = new PinPongMan();
p.run();
}
}
}
public interface SportManInter {
/**
静态方法
接口的静态方法,必须接口名自己调用
*/
static void inAddr() {
System.out.println("静态方法");
}
}
class PingPong implements SportManInter {
}
class Test {
public static void main(String[] args) {
SportManInter.inAddr();
}
}
public interface SportManInter {
/**
私有方法(实例方法)
必须在接口内部才能被访问
*/
private void go() {
System.out.println("私有方法");
}
default void run() {
go();
}
}
class PingPong implements SportManInter {
}
class Test {
public static void main(String[] args) {
PingPong p = new PingPong();
p.run();
}
}