【Java】中级篇:面向对象编程(终章)

一、关键字static

静态变量/类变量

静态变量内存解析:

静态方法/类方法

tips:

main()方法剖析

二、代码块(初始化块)

三、关键字final

final修饰类表示此类不可被继承;修饰方法表示此方法不能被重写;修饰变量表示此变量一旦赋值就不可更改。

当static与final一起修饰成员变量时,此变量称为全局常量

四、抽象类与抽象方法

举例:

语法格式

抽象类

[权限修饰符] abstract class 类名{

}

抽象方法

[权限修饰符] abstract 返回值类型 方法名{

}

代码举例

public class Test {
    
    public static void main(String[] args) {
        Circle circle=new Circle(5);
        circle.CalculateS();
        circle.CalculateC();
        
    }
}

abstract class GObject{
    public abstract void CalculateS();
    //求面积
    public abstract void CalculateC();
    //求周长
}

class Circle extends GObject{
    int r;
    public Circle(int r){
        this.r=r;
    }
    {
        System.out.println("This is a circle!");
    }

    public void CalculateS(){
        System.out.println("S="+Math.PI*r*r);
    }
    
    public void CalculateC(){
        System.out.println("C="+2*r*Math.PI);
    }
}

运行结果:

具体使用说明

tips:

1、abstract不能修饰属性、构造器、代码块等。

2、

五、接口(关键字interface)

接口本质:契约、标准、规范

接口内部结构

接口与类的关系:实现关系。

格式:class A implements B,C{

}

说明

接口和接口的关系:继承关系,而且可以多继承。

代码举例

public class Test {
    
    public static void main(String[] args) {
        D d=new D(2, 1);
        d.AA();
        d.BB();
        
    }
}

interface A{
    void AA();
}
interface B{
    void BB();
}

interface C extends A,B{}

class D implements C{
    int a;
    int b;
    public D(int a,int b){
        this.a=a;
        this.b=b;
    }
    public void AA(){
        System.out.println(a+b);
    }
    public void BB(){
        System.out.println(a-b);
    }
}

运行结果:

接口多态性

public class Test {
    
    public static void main(String[] args) {
        //接口多态性
        A a=new D(2, 1);
        a.AA();
        
    }
}

interface A{
    void AA();
}
interface B{
    void BB();
}

interface C extends A,B{}

class D implements C{
    int a;
    int b;
    public D(int a,int b){
        this.a=a;
        this.b=b;
    }
    public void AA(){
        System.out.println(a+b);
    }
    public void BB(){
        System.out.println(a-b);
    }
}

运行结果:

六、内部类

内部类:将一个类A定义在另一个类B中,类A就是内部类(InnerClass),类B就是外部类(OuterClass)。

声明内部类原因

分类

1、成员内部类

创建成员内部类的实例

public class Test {
    
    public static void main(String[] args) {
        Animal.DeadAnimal deadAnimal=new Animal.DeadAnimal();
        deadAnimal.deadtime();

        Animal animal=new Animal();
        Animal.Bird bird=animal.new Bird();
        bird.live();
    }
}

class Animal{
    //静态成员内部类
    static class DeadAnimal {
        public void deadtime(){
            System.out.println("这只动物死了");
        }
    }

    //非静态~~~~~
    class Bird{
        public void live(){
            System.out.println("小鸟啾啾");
        }
    }
}

运行结果:

2、局部内部类

创建成员内部类

public class Test {
    
    public static void main(String[] args) {
        Animal animal=new Animal();
        animal.live().talk();
    }
}

class Animal{
    public T live(){
        //若方法返回值是一个接口,在没有子类或者不知道子类的情况下,
        //在方法中创建一个局部内部类,其他地方不需要用,仅在方法中使用
        class cat implements T{
            public void talk(){
                System.out.println("小猫喵喵");
            }

        }

        return new cat();
    }
}

interface T{
    void talk();
}

运行结果:

七、枚举

枚举类型本质上也是一种类,只不过类的对象是有限的、固定的几个,不能让用户随意创建。

在开发中,如果针对某个类,其类的实例是确定的,建议将此类声明为枚举类。

使用关键字enum

public class Test {
    
    public static void main(String[] args) {
        System.out.println(Season.SPRING);
        System.out.println(Season.SUMMER);
        System.out.println(Season.AUTUMN);
        System.out.println(Season.WINTER);
    }
}
enum Season{
    
    SPRING("春","暖"),
    SUMMER("夏","热"),
    AUTUMN("秋","凉"),
    WINTER("冬","冷");
    private final String name;
    private final String feel;

    private Season(String name,String feel){
        this.feel=feel;
        this.name=name;
    }

    public String getName() {
        return name;
    }
    public String getFeel() {
        return feel;
    }

    public String toString(){
        return "季节名:"+name+",让人感觉"+feel;
    }

}

运行结果:

enum中常用方法:

八、注解(Annotation)

常见注解作用

元注解:对现有注解进行解释说明的注解

框架 = 注解 + 反射 + 设计模式

了解部分元注解

九、单元测试

测试分类

正确编写单元测试方法满足条件

    @Test
    public void test1(){//单元测试方法
        System.out.println("!!!!!");
    }

十、包装类

 基本数据类型与包装类之间的转换

public class Test {
  
    public static void main(String[] args) {
        //基本数据类型(int举例)转换为包装类
        //->法①使用包装类的构造器
        //->②使用VualueOf方法
        int i1=10;
        Integer i2 = new Integer(i1);
        Integer i3 = Integer.valueOf(i1);
        System.out.println(i2.toString());
        System.out.println(i3.toString());

        //包装类转换为基本数据类型
        Integer i4 = new Integer(15);
        int i5 = i4.intValue();

        Float f1 = new Float(12.1f);
        float f2 = f1.floatValue();
        System.out.println(i5);
        System.out.println(f2);
    }
}

运行结果:

 

String与基本数据类型、包装类之间的转换

public class Test {
  
    public static void main(String[] args) {
        //String类型转换为基本数据类型(int举例)、包装类
        //调用包装类的静态方法:parseXxx()
        String str3 = "112";
        int i2 = Integer.parseInt(str3);
        System.out.println(i1);

        //包装类、基本数据类型转换为String类型
        //方式一:调用String的重载的静态方法ValueOf
        int i1 = 10;
        String str1 = String.valueOf(i1);
        System.out.println(str1);
        //方式二:基本数据类型+""
        String str2 = i1+"";
        System.out.println(str2);

    }
}

运行结果:

 

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值