java知识点杂货铺【六】

一、实现一个沙发床的实例

 题目的来源是这样的,汽车素有沙发床的称誉,也就是说如果要写一个汽车的类那么也就是说汽车这个类要继承沙发、床类,但是这里由于Java类设计特性,我们这里把沙发、和床设计成接口,然后实现这两个接口
代码:

public class CarDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Car car = new Car();
        car.sit();
        car.sleep();
    }

}
  //定义沙发接口
interface Sofa{
    public void sit();
}
  //定义床接口
interface Bed{
    public void sleep();
}
    //定义车类
class Car implements Sofa,Bed{

    @Override
    public void sleep() {
        System.out.println("implement interface sleep function");

    }

    @Override
    public void sit() {
        System.out.println("implement interface sit function");

    }

}

运行结果:

implement interface sit function
implement interface sleep function

二、java适配器实例

关于java适配器模式,需要的可以点击链接,这里就不再重复造轮子了
《JAVA与模式》之适配器模式
实例代码入下:

package com.zhanghaiping.javaHomeWork;

interface Student{
    public void hardStudy();
    public void readBook();
    public void inTimeEating();
    public void noLove();
    public void inTimeSleep();
}

    ///定义适配器
abstract class Adapter implements Student{

    @Override
    public void hardStudy() {
    }

    @Override
    public void readBook() {

    }
    @Override
    public void inTimeEating() {

    }

    @Override
    public void noLove() {

    }

    @Override
    public void inTimeSleep() {

    }

    //自己拥有的独特属性
    public void smoking(){

    }

    public void casual() {

    }

}

class Zhanghaiping extends Adapter{

    @Override
    public void noLove() {
        super.noLove();
        System.out.println("只有学生的一个不恋爱的属性");
    }

    @Override
    public void smoking() {
        super.smoking();
        System.out.println("抽烟");

    }

    @Override
    public void casual() {

        super.casual();
        System.out.println("随意");
    }   
}

public class AdapterDemo{

    public static void main(String[] args) {

        Zhanghaiping zhanghaiping = new Zhanghaiping();

        zhanghaiping.noLove();
        zhanghaiping.smoking();
        zhanghaiping.casual();
    }   

}

运行结果:

只有学生的一个不恋爱的属性
抽烟
随意

三、java instanceof 关键字详解和实例

  Java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。
用法:
  result = object instanceof class
参数:
  Result:布尔类型。
  Object:必选项。任意对象表达式。
  Class:必选项。任意已定义的对象类。
说明:
  (1)一个类的实例包括本身的实例,以及所有直接或间接子类的实例
  (2)instanceof左边操作元显式声明的类型与右边操作元必须是同种类或右边是左边父类的继承关系,
  (3)不同的继承关系下,编译出错
  代码实例:
  

package com.zhanghaiping.javaHomeWork;

 //定义接口 Person,空实现
interface Person{}

 /定义抽向类  Boy 实现 Person,空实现
abstract class Boy implements Person{}

/定义普通类 Zhanghaiping1 继承 Boy ,空实现
class Zhanghaiping1 extends Boy{}

public class InstanceofDemo {

    public static void main(String[] args) {

        boolean result;

        // 第一种
        Person person = null;
        Boy boy = null;
        Zhanghaiping1 zhanghaiping1 = null;

        System.out.println("-------instanceof 测试一: ---------");

        result = person instanceof Person;
        System.out.println("person instanceof Person = " + result);

        result = boy instanceof Boy;
        System.out.println("boy instanceof Boy = " + result);

        result = zhanghaiping1 instanceof Zhanghaiping1;
        System.out.println("zhanghaiping instanceof Zhanghaiping = " + result);

        // 第二种
        person = new Person(){};
        boy = new Boy() {};
        zhanghaiping1 = new Zhanghaiping1();

        System.out.println("-------instanceof 测试二: ---------");

        result = person instanceof Person;
        System.out.println("person instanceof Person = " + result);

        result = boy instanceof Boy;
        System.out.println("boy instanceof boy = " + result);

        result = zhanghaiping1 instanceof Zhanghaiping1;
        System.out.println("zhanghaiping instanceof Zhanghaiping1 = " + result);

        //第三种,接下来才是重点哦!
        person = new Boy() {};
        boy = new Zhanghaiping1();

        System.out.println("-------instanceof 测试三: ---------");

        result = person instanceof Person;
        System.out.println("person instanceof Person = " + result);

        result = person instanceof Boy;
        System.out.println("person instanceof Boy = " + result);

    }

}

运行结果:

-------instanceof 测试一: ---------
person instanceof Person = false
boy instanceof Boy = false
zhanghaiping instanceof Zhanghaiping = false
-------instanceof 测试二: ---------
person instanceof Person = true
boy instanceof boy = true
zhanghaiping instanceof Zhanghaiping1 = true
-------instanceof 测试三: ---------
person instanceof Person = true
person instanceof Boy = true

四、Java Exception异常,RuntimeException、IOException实例

4.1 try cathch到异常时,如有finally语句,那么finally语句是否还是会执行?我们来用代码说话:
代码实例:

package com.zhanghaiping.javaHomeWork;

public class ExceptionDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] testArr = {1,2};

        try {
            int temp = testArr[3];
        } catch (Exception e) {
            System.out.println("捕捉到异常执行语句catch");
        }finally{
            System.out.println("你说finally语句执行了吗?");
        }
    }

}

运行结果:

捕捉到异常执行语句catch
你说finally语句执行了吗?

4.2 catch到异常且catch语句块里面有return语句,那么finally语句是否还是会执行呢?代码来说明:
代码实例:

package com.zhanghaiping.javaHomeWork;

public class ExceptionDemo {

    public static void main(String[] args) {

        ExceptionDemo exceptionDemo = new ExceptionDemo();

        int tempInt = 1;
        tempInt =exceptionDemo.testFunc();

        System.out.println("函数返回后tempInt的值为: " + tempInt);

    }

    @SuppressWarnings("finally")
    public int testFunc() {
        int[] testArr = {1,2};
        int i = 0;

        try {
            int temp = testArr[3];
        } catch (Exception e) {
            i = 2;
            System.out.println("捕捉到异常执行语句catch");
            return i += 1;
        }finally{
            System.out.println("你说finally语句执行了吗?");
            System.out.println("此时i的值为: " + i);
            return i += 1 ;
        }
    }
}

运行结果:

捕捉到异常执行语句catch
你说finally语句执行了吗?
此时i的值为: 3
函数返回后tempInt的值为: 4

4.3 try catch 嵌套相关问题,这个问题比较深刻
看代码:

package com.zhanghaiping.javaHomeWork;

import java.io.FileInputStream;

public class ExceptionDemo {

    public static void main(String[] args) {

        ExceptionDemo exceptionDemo = new ExceptionDemo();

        int tempInt = 1;
        tempInt = exceptionDemo.testFunc();

        System.out.println("函数返回后tempInt的值为: " + tempInt);

    }

    @SuppressWarnings("finally")
    public int testFunc() {
        int[] testArr = {1,2};
        int i = 0;

        try {
            int temp = testArr[3];
        } catch (Exception e) {
            System.out.println("外层catch到异常,catch语句执行:");

            ///////////try catch嵌套
            try {
                FileInputStream fileInputStream = new FileInputStream("F:/学习/不存在的文件.txt");
            } catch (Exception e2) {
                System.out.println("内层catch语句执行,异常类型为FilNotFoundException ");
                i += 1;
                System.out.println("内层嵌套catch中此时i的值为: " + i);

                return i += 10;

            }finally{
                System.out.println("内层嵌套finally语句执行:");
                System.out.println("内层finally语句此时i的值为: " + i);

                return i += 20;
            }

            //return i += 1;

        }finally{
            System.out.println("外层嵌套finally语句执行: ");
            System.out.println("外层finally语句此时i的值为: " + i);
            return i += 2 ;
        }
    }
}

运行结果:

外层catch到异常,catch语句执行:
内层catch语句执行,异常类型为FilNotFoundException 
内层嵌套catch中此时i的值为: 1
内层嵌套finally语句执行:
内层finally语句此时i的值为: 11
外层嵌套finally语句执行: 
外层finally语句此时i的值为: 31
函数返回后tempInt的值为: 33

总结

 这一次的知识点感觉都比较烧脑,但是在平常中我们又都容易忽略或者根本就想不到的问题。
 沙发床的这个实例体现了Java的抽象类和接口设计特性,抽象类一般是相对于整体性,而接口就是针对于具体性的,什么意思呢?比如说我们要设计一个水上飞机的类,这个类它既有飞机飞的特性,又具有船在水上漂浮的特性,那么我们此刻应该就这样设计,将飞的特性和船漂浮的特性各设计一个借口,然后去实现这两个接口,而不是设计一个飞机的抽象类或者一个船的类而去继承他们。
 Java的适配器设计模式体现的就是一个有些特别案例,我们平常有很多接口,然后接口包含了各种各样的方法,多姿多彩,但是我们可能只用的到里面的几个方法,难道就要我们写一个类然后将里面的方法全都实现吗?这显然会导致代码臃肿,也不符合代码复用的特性,所以就产生了这样的适配器设计模式。
 关于Java异常的这些问题,要是在平常我们肯定都想不到,catch语句中有返回语句了那么finally语句到底执不执行?上面这一段代码深刻的体现了出来,仔细研究研究。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值