java作业代码

homework1:

class Phone{

String phone_num;

public Phone(String str_num){
phone_num = str_num;
}
public void call(){
System.out.println("我有打电话的功能");
}
}


class NewPhone extends Phone{
public NewPhone(String phone_num2){
super(phone_num2);
}

public void call(){
super.call();
}
public void call_reload(){
System.out.println("我不仅有打电话的功能,还能开启语言和关闭语言");
}
}

homework2:
public class work1{
public static void main(String[] args){
NewPhone p1 = new NewPhone("15562359845");
p1.call();
p1.call_reload();
}

}



interface ElephantHome{
void open();
void putin();
void putout();
void close();
}


class Fridge implements ElephantHome{
public void open(){
System.out.println("我在Fridge类里面实现了open方法");
}
public void putin(){
System.out.println("我在Fridge类里面实现了将大象放入冰箱的方法");
}
public void putout(){
System.out.println("我在Fridge类里面实现了将大象拿出冰箱的方法");
}
public void close(){
System.out.println("我在Fridge类里面实现了close方法");
}
}


class WashMachine implements ElephantHome{
public void open(){
System.out.println("我在WashMachine类里面实现了open方法");
}
public void putin(){
System.out.println("我在WashMachine类里面实现了将大象放入洗衣机的方法");
}
public void putout(){
System.out.println("我在WashMachine类里面实现了将大象拿出洗衣机的方法");
}
public void close(){
System.out.println("我在WashMachine类里面实现了close方法");
}
}


class Elephant{
public void choose(ElephantHome machine){
machine.putin();
machine.putout();
}
}


public class Test02{
public static void main(String[] args){
Fridge f1 = new Fridge();
WashMachine w1 = new WashMachine();
Elephant e1 = new Elephant();
e1.choose(f1);
e1.choose(w1);
}

}

homework3:

import java.util.Scanner;
class MyException extends Exception{
public MyException(String message){
super(message);
}
}


public class Test03{
public static void main(String[] args){
try{
Scanner sc = new Scanner(System.in);
System.out.println("请输入除数和被除数");
int a = sc.nextInt();
int b = sc.nextInt();
divide(a,b);
}catch(MyException e){
System.out.println(e.getMessage());
}
}
public static void divide(int a,int b) throws MyException{
if(b==0){
throw new MyException("这是我自己定义的异常类,被除数是负数");
}else{
int result = a/b;
System.out.println("除法成功");
System.out.println(result);
}
}

}

homework4:

class Demo implements Runnable{
Object lock = new Object();
public void run(){

synchronized(lock){
System.out.println(Thread.currentThread().getName()+"上锁");
for(int i=1;i<=3;i++){
String str=i+"";
System.out.println(Thread.currentThread().getName()+"使用中,循环次数为:"+str);
}
System.out.println(Thread.currentThread().getName()+"出来");
}
}
}


public class work4{
public static void main(String[] args){
Demo demo = new Demo();
new Thread(demo,"张三").start();
new Thread(demo,"李四").start();
}
}

猜你喜欢

转载自blog.csdn.net/f156207495/article/details/80258207
今日推荐