1.接口
概念
接口,使用interface修饰,是个特殊的抽象类,当抽象类里只有静态常量或抽象方法时,可以用接口代替
注意:
接口里可以有静态常量或抽象方法,JDK1.8新特性还添加了默认方法和静态方法接口可以继承多个接口一个类只能继承一个父类,可以实现多个接口接口不能new对象
具体使用1
public class Test {
public static void main(String
[] args
) {
MyClass m
= new MyClass();
m
.defaultMethod();
I1
.staticMethod();
}
}
interface I1{
public abstract void i1Method();
default void defaultMethod(){
System
.out
.println("I1中的默认方法");
}
public static void staticMethod(){
System
.out
.println("I1中的静态方法");
}
}
interface I2 extends I3,I4
{
public abstract void i2Method();
}
interface I3{
public abstract void i3Method();
}
interface I4{
public abstract void i4Method();
}
class MyClass implements I1,I2
{
@Override
public void i3Method() {
System
.out
.println("实现I3中的抽闲抽象方法");
}
@Override
public void i4Method() {
System
.out
.println("实现I4中的抽闲抽象方法");
}
@Override
public void i2Method() {
System
.out
.println("实现I2中的抽闲抽象方法");
}
@Override
public void i1Method() {
System
.out
.println("实现I1中的抽闲抽象方法");
}
}
具体使用2
interface IPerson{
public abstract void eat();
}
class Chinese implements IPerson{
@Override
public void eat() {
System
.out
.println("中国人吃山珍海味");
}
}
class Japanese implements IPerson{
@Override
public void eat() {
System
.out
.println("日本人吃马赛克");
}
}
2.类与接口之间的关系
关系描述
类与类单继承类与接口多实现接口与接口多继承
3.多态
概念
多种形态
类的多态
public class Test {
public static void main(String
[] args
) {
Teacher t
= new Teacher();
Vehicle v
= new Car();
t
.open(v
);
System
.out
.println("老师欣赏沿途的风景...");
t
.close(v
);
}
}
class Teacher{
public void open(Vehicle v
){
v
.start();
}
public void close(Vehicle v
){
v
.stop();
}
}
class Car extends Vehicle{
@Override
public void start() {
System
.out
.println("汽车开");
}
@Override
public void stop() {
System
.out
.println("汽车停");
}
}
class Bike extends Vehicle{
@Override
public void start() {
System
.out
.println("自行车开");
}
@Override
public void stop() {
System
.out
.println("自行车停");
}
}
abstract class Vehicle{
public void start();
public void stop();
}
接口的多态
public class Test {
public static void main(String
[] args
) {
Computer com
= new Computer();
IUSB usb
= new Disk();
com
.connection(usb
);
}
}
class Computer{
public void connection(IUSB usb
){
usb
.use();
}
}
class Mouse implements IUSB{
@Override
public void use() {
System
.out
.println("鼠标:左点点、右点点");
}
}
class Disk implements IUSB{
@Override
public void use() {
System
.out
.println("硬盘:上传文件、下载文件");
}
}
interface IUSB{
public void use();
}
4.对象转型
简介
引用数据的转型
向上转型:子类 转 父类类型
向下转型:父类 转 子类类型,又称为引用数据类型的强转
对象的向上转型
向上转型就是多态
注意:
可以直接调用父类非私有化属性可以直接调用父类非私有化方法 3. 可以直接调用子类重写父类的方法 向上转型即多态的缺点:不能调用子类的属性和方法
public class Test {
public static void main(String
[] args
) {
Father f
= new Son();
System
.out
.println(f
.fatherStr
);
f
.fatherMethod01();
f
.fatherMethod02();
}
}
class Father{
String fatherStr
= "父类属性";
public void fatherMethod01(){
System
.out
.println("父类方法01");
}
public void fatherMethod02(){
System
.out
.println("父类方法02");
}
}
class Son extends Father{
String fatherStr
= "子类和父类同名的属性";
String sonStr
= "子类属性";
public void sonMethod(){
System
.out
.println("子类方法");
}
@Override
public void fatherMethod02() {
System
.out
.println("子类重写父类方法02");
}
}
对象的向下转型
向下转型一定要用instanceof判断对象类型
public class Test {
public static void main(String
[] args
) {
Animal an
= new Dog();
if(an
instanceof Dog){
Dog dog
= (Dog
) an
;
}else if(an
instanceof Cat){
Cat cat
= (Cat
) an
;
}
}
}
class Animal{
}
class Dog extends Animal{
}
class Cat extends Animal{
}
5.内部类
概念
一个类里声明另外一个类
分类
成员内部类
public class Test01 {
public static void main(String
[] args
) {
Inner inner
= new Outter().new Inner();
inner
.print();
}
}
public class Outter {
private String str1
= "aaa";
String str2
= "222";
protected String str3
= "333";
public String str4
= "444";
static String str5
= "555";
final String str6
= "666";
public class Inner{
private String str1
= "bbb";
public void print(){
System
.out
.println("成员内部类里的方法");
System
.out
.println(Outter
.this.str1
);
System
.out
.println(str1
);
System
.out
.println(str2
);
System
.out
.println(str3
);
System
.out
.println(str4
);
System
.out
.println(str5
);
System
.out
.println(str6
);
}
}
}
静态内部类
public class Test01 {
public static void main(String
[] args
) {
Inner inner
= new Outter.Inner();
inner
.print();
}
}
public class Outter {
private String str1
= "aaa";
String str2
= "222";
protected String str3
= "333";
public String str4
= "444";
static String str5
= "555";
final String str6
= "666";
public static class Inner{
public void print(){
System
.out
.println("静态内部类的方法");
System
.out
.println(str5
);
}
}
}
接口内部类
public class Test01 {
public static void main(String
[] args
) {
Inner inner
= new IOutter.Inner();
inner
.print();
}
}
public interface IOutter {
public class Inner{
public void print(){
System
.out
.println("接口内部类的方法");
}
}
}
局部内部类
public class Test01 {
public static void main(String
[] args
) {
Outter outter
= new Outter();
outter
.method();
}
}
public class Outter {
public void method(){
class Inner{
public void print(){
System
.out
.println("局部内部类的方法");
}
}
Inner inner
= new Inner();
inner
.print();
}
}
public class Test01 {
public static void main(String
[] args
) {
Outter outter
= new Outter();
Object obj
= outter
.method();
System
.out
.println(obj
);
}
}
public class Outter {
public Object
method(){
String str
= "abc";
class Inner{
@Override
public String
toString() {
return str
;
}
}
return new Inner();
}
}
匿名内部类
public class Test01 {
public static void main(String
[] args
) {
A a
= new A() {
@Override
public void method() {
System
.out
.println("xxxx");
}
};
I1 i1
= new I1() {
@Override
public void method() {
System
.out
.println("xxxx");
}
};
}
}
abstract class A{
public abstract void method();
}
interface I1{
public abstract void method();
}