Java笔试知识点一

一、接口和抽象类的区别?

1.相同点:

都不能被实例化

接口实现和抽象类的子类都只有实现了接口或者抽象类中的方法后才能被实例化

2.不同点

实现接口的关键字为implements,继承抽象类的关键字为extends

一个类可以实现多个接口,只能继承一个抽象类

接口方法默认修饰符是public,抽象方法可以有public、protected和default这些修饰符

二、实现多线程的方法

1.实现Runnable接口,实现run方法

class MyThread implements Runnable{
//创建线程类
    public void run() {
        System.out.println("Thread body");
    }
}

public class Test{
    public static void main(String[] args){
        MyThread thread = new MyThread();
        Thread t = new Thread(thread);
        t.start();//开启线程
    }
}

2.继承Thread类,重写run方法

class MyThread extends Thread {
    public void run() {
        System.out.println("Thread body");
    }
}

public class Test {
    public static void main(String[] args) {
        Mythread thread = new MyThread();
        thread.start();
    }
}

三、利用递归方法实现6!

public class Test {

    public static long fac(int n){
        if(n > 1)
            return (n*fac(n-1));
        else
        return 1;
    }

    public static void main(String[] args) {
        System.out.println(fac(6));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值