"Hundred Days, Hundred Questions · Basics" Prepare for the interview, insist on brushing up the questions Chapter 7 - Encapsulation and inheritance!

This column "Hundred Days and Hundred Questions" has been updated for a long time, join this question brushing plan and grow together!


JAVA19 modify the definition of the Data class [encapsulation]

topic:
insert image description here

answer:

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 authentication age [package]

topic:
insert image description here

answer:

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 Completion Constructor [Inheritance]

topic:
insert image description here

answer:

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 rewrites calculation logic [inheritance]

topic:
insert image description here

answer:

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());
        }
    }

Guess you like

Origin blog.csdn.net/apple_51673523/article/details/126510004