JAVA面向对象编程---零散知识点三

一、匿名对象

通常我们创建好一个对象都会把它赋值给一个变量,如果没有赋值给变量该对象则为匿名对象。

package demo10;

/**
 * @Author:张金贺
 * @Date:2022/6/23 15:10
 * @Version 1.0
 */
public class Animal {
    private String color;

    public void eat(){
        System.out.println("吃东西");
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}
package demo10;

/**
 * @Author:张金贺
 * @Date:2022/6/23 15:11
 * @Version 1.0
 */
public class test {
    public static void main(String[] args) {
//        Animal animal =new Animal();
        new Animal();//匿名对象
        new Animal().eat();//可以直接调用
        show(new Animal());//匿名对象作为方法的参数
        find();
    }
    public static void show(Animal animal){
        animal.eat();
    }
    public static Animal find(){
        return new Animal();//匿名对象作为方法的返回值
    }
}

二、内部类

1.成员内部类

package demo11;

/**
 * @Author:张金贺
 * @Date:2022/6/23 15:17
 * @Version 1.0
 */
public class Computer {//外部类
    private boolean status =true;//电脑的状态
    public class cpu{//内部类
        //可以访问外部类成员变量
        public void run(){
            System.out.println("电脑状态"+status);
            System.out.println("CPU正在运转");
        }
    }
}
package demo11;

/**
 * @Author:张金贺
 * @Date:2022/6/23 15:20
 * @Version 1.0
 */
public class test {
    public static void main(String[] args) {
        //创建内部类对象
        Computer.cpu cpu =new Computer().new cpu();
        cpu.run();
    }
}

2.局部内部类

package demo12;

/**
 * @Author:张金贺
 * @Date:2022/6/23 15:25
 * @Version 1.0
 */
public class Computer {
    //打开显示器
    public void openDisplay(){
        //显示器类
        class Display{
            public void open(){
                System.out.println("打开显示器!");
            }
        }
        new Display( ).open();}
}

3.匿名内部类

package demo13;

/**
 * @Author:张金贺
 * @Date:2022/6/23 15:27
 * @Version 1.0
 */
public abstract class Animal {
    public abstract void eat();
}
package demo13;

/**
 * @Author:张金贺
 * @Date:2022/6/23 15:28
 * @Version 1.0
 */
public class test {
    public static void main(String[] args) {
        //匿名内部类
        //继承Animal类,并且创建该类的对象
        Animal animal =new Animal() {
            @Override
            public void eat() {
                System.out.println("吃东西");
            }
        };
        animal.eat();
    }
}

三、知识点集合

1.定义在成员变量位置每创建一个对象,构造代码块({   })执行一次。

2.静态代码块(static{   })也定义在成员变量位置,一般用于给静态变量赋值,在main方法和构造方法前执行。

3.执行顺序:静态代码块-->构造代码块-->构造函数。

4.枚举:本质上也是一个类,class-->enum

/**
 * @Author:张金贺
 * @Date:2022/6/23 15:50
 * @Version 1.0
 */
public enum Sex {
    男,女
}
/**
 * @Author:张金贺
 * @Date:2022/6/23 15:51
 * @Version 1.0
 */
public class sextest {
    public static void main(String[] args) {
        Sex sex = Sex.男;
        System.out.println(sex);
    }
}

5.函数式接口

lamda表达式:(参数)->sout(实现接口中的方法);//接口中只能有一个方法

package demo14;

/**
 * @Author:张金贺
 * @Date:2022/6/23 16:01
 * @Version 1.0
 */
public interface IADD {
    int add(int num1,int num2 );
}
package demo14;

/**
 * @Author:张金贺
 * @Date:2022/6/23 16:02
 * @Version 1.0
 */
public class test {
    public static void doadd(int num1,int num2,IADD cal){
        cal.add(num1, num2);
        System.out.println(cal.add(num1, num2));
    }

    public static void main(String[] args) {
        //匿名内部类
        doadd(3, 5, new IADD() {
            @Override
            public int add(int num1, int num2) {
                return num1+num2;
            }
        });
        //lamda表达式
        doadd(3,5,(a,b)->{return a+b;});
    }
}

->如果只有一个return,则可以忽略。

lamda表达式可以作为方法的返回值。

package demo14;

/**
 * @Author:张金贺
 * @Date:2022/6/23 16:12
 * @Version 1.0
 */
public class fa {
    public static IADD getAdd(){
        return (a,b)->a+b;
    }

    public static void main(String[] args) {
        IADD add=getAdd();
        System.out.println(add.add(3,5));
    }
}

6.成员变量和方法参数的选择:如果该变量是该类的一部分则定义成成员变量,如果它只是功能参与计算的一个数则定义成方法的参数。

7.  Lambda表达式与面向对象的综合应用

一名p8工程师有姓名和年龄属性,还有乘坐交通工具去上班的方法;为了出行方便购买了一辆飞鸽自行车、一辆雅迪电动车、一辆奔驰矫车.自行车,电动车和轿车都有颜色和品牌属性,轿车还有特有的属性车牌号,三辆车都可以开动。
电动车和轿车可以增加动力,电动车增加动力的方式是充电,轿车增加动力的方式是加油。
根据以上需求分别编写对应的类并测试。

package demo15lamda;

/**
 * @Author:张金贺
 * @Date:2022/6/23 16:31
 * @Version 1.0
 */
public abstract class Vehicle {
    private String color;
    private String brand;

    public abstract void run();

    public Vehicle(String color, String brand) {
        this.color = color;
        this.brand = brand;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }
}
package demo15lamda;

/**
 * @Author:张金贺
 * @Date:2022/6/23 16:32
 * @Version 1.0
 */
public class Bicycle extends Vehicle{
    public Bicycle(String color, String brand) {
        super(color, brand);
    }

    @Override
    public void run() {
        System.out.println(this.getColor()+"颜色的"+this.getBrand()+"品牌的自行车开动了");
    }
}
package demo15lamda;

/**
 * @Author:张金贺
 * @Date:2022/6/23 16:34
 * @Version 1.0
 */
public class ElectricVehicle extends Vehicle{
    public ElectricVehicle(String color, String brand) {
        super(color, brand);
    }

    @Override
    public void run() {
        System.out.println(this.getColor()+"颜色的"+this.getBrand()+"品牌的电动车开动了");
    }
}
package demo15lamda;

/**
 * @Author:张金贺
 * @Date:2022/6/23 16:35
 * @Version 1.0
 */
public class Car extends Vehicle{
    private String cardNumber;

    public Car(String color, String brand,String cardNumber) {
        super(color, brand);
        this.cardNumber=cardNumber;
    }

    public String getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }

    @Override
    public void run() {
        System.out.println(this.getColor()+"颜色的"+this.getBrand()+"品牌的"+"车牌号为"+this.cardNumber+"的小轿车开动了");
    }
}
package demo15lamda;

/**
 * @Author:张金贺
 * @Date:2022/6/23 16:40
 * @Version 1.0
 */
public class Developer {
    private String name;
    private Integer age;
    public void takingVehicle(Vehicle vehicle){
        vehicle.run();
    }

    public Developer(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
package demo15lamda;

/**
 * @Author:张金贺
 * @Date:2022/6/23 16:42
 * @Version 1.0
 */
public class test {
    public static void main(String[] args) {
        Bicycle bicycle=new Bicycle("红","飞鸽");
        ElectricVehicle electricVehicle=new ElectricVehicle("白","亚迪");
        Car car=new Car("黑","奔驰","888888");
        Vehicle[] vehicles={bicycle,electricVehicle,car};
        for (Vehicle vehicle : vehicles) {
            vehicle.run();
        }
    }
}

8.异常

数组下标越界异常ArrayIndexOutOfBoundsException

异常体系:

throwable :
error:错误-严重的问题
exception:异常-我们可以解决的问题
runtimeException:所有继承了runtimeException的类都叫做运行时异常-程序在运行期间产生的异常编译时异常:在编译期间产生的异常,我们必须处理

捕获异常:

try { 可能发生异常的代码 } 

catch ( 可能发生异常的类型 )  { sout ( 打印异常提示 ) return -1 }

   catch ( 可能发生异常的类型 )  { sout ( 打印异常提示 ) return -1 }

      catch ( 可能发生异常的类型 )  { sout ( 打印异常提示 ) return -1 }

finally { finally 代码块中的代码无论是否发生异常都会被执行 }

抛出异常:

声明异常:告诉方法的调用者这个方法有可能发生异常,让方法的调用者来处理该异常。

自定义异常(类):

/**
 * @Author:张金贺
 * @Date:2022/6/23 17:23
 * @Version 1.0
 */
public class demoE extends Exception{
    public demoE() {
    }

    public demoE(String message) {
        super(message);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java张金贺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值