『百日百题 · 基础篇』备战面试,坚持刷题 第七话——封装与继承!

本专栏『百日百题』长期更新,一起加入本刷题计划,一起成长吧!


JAVA19 修改Data类的定义【封装】

题目:
在这里插入图片描述

题解:

import java.util.Scanner;

public class Main {
    
    

    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
    
    
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            Data data = new Data(x, y);
            System.out.println(data.getX() + data.getY());
        }
    }

}

class Data {
    
    
    private int x;
    private int y;

    public Data(int x, int y) {
    
    
        this.x = x;
        this.y = y;
    }

    public int getX() {
    
    
        return x;
    }

    public int getY() {
    
    
        return y;
    }

}

JAVA20 验证年龄【封装】

题目:
在这里插入图片描述

题解:

import java.util.Scanner;

public class Main {
    
    

    public static void main(String[] args) {
    
    
        Person p = new Person();
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
    
    
            int age = scanner.nextInt();
            p.setAge(age);
            System.out.println(p.getAge());
        }
    }

}

class Person {
    
    

  private int age;
    //write your code here......
       public int getAge() {
    
    
        return age;
    }

    public void setAge(int age) {
    
    
        if (age >200 ) {
    
    
            this.age = 200;
        } else if (age >= 0 && age <= 200) {
    
    
            this.age = age;
        } else if(age <0) {
    
    
            this.age = 0;
        }
    }
  } 



JAVA21 补全构造方法【继承】

题目:
在这里插入图片描述

题解:

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
    
    
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            int z = scanner.nextInt();
            Sub sub = new Sub(x, y, z);
            System.out.println(sub.calculate());
        }
    }

}

class Base {
    
    
    private int x;
    private int y;

    public Base(int x, int y) {
    
     //父类构造方法
        this.x = x;
        this.y = y;
    }

    public int getX() {
    
    
        return x;
    }

    public int getY() {
    
    
        return y;
    }
}

class Sub extends Base {
    
    
    private int z;

    public Sub(int x, int y, int z) {
    
     //子类构造方法
        super(x, y); //调用父类构造方法
        this.z = z;
    }

    public int getZ() {
    
    
        return z;
    }

    public int calculate() {
    
     //子类计算三者乘积
        return super.getX() * super.getY() * this.getZ();
    }
}

JAVA22 重写计算逻辑【继承】

题目:
在这里插入图片描述

题解:

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
    
    
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            Sub sub = new Sub(x, y);
            sub.calculate();
        }
    }

}

class Base {
    
    
    private int x;
    private int y;

    public Base(int x, int y) {
    
    
        this.x = x;
        this.y = y;
    }

    public int getX() {
    
    
        return x;
    }

    public int getY() {
    
    
        return y;
    }

    public void calculate() {
    
    
        System.out.println(getX() * getY());
    }
}

class Sub extends Base {
    
    
    public Sub(int x,int y){
    
    
        super(x,y);
    }
    public void calculate() {
    
    
        if(getY()==0){
    
    
             System.out.println("Error");
        }else{
    
    
        System.out.println(getX() / getY());
        }
    }

猜你喜欢

转载自blog.csdn.net/apple_51673523/article/details/126510004