列表

详情


JAVA27. 重写父类方法

描述

父类Base中定义了若干get方法,以及一个sum方法,sum方法是对一组数字的求和。请在子类 Sub 中重写 getX() 方法,使得 sum 方法返回结果为 x*10+y

输入描述

整数

输出描述

整数的和

示例1

输入:

1 2

输出:

12

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Java 解法, 执行用时: 13ms, 内存消耗: 9400KB, 提交时间: 2022-04-29

import java.util.Scanner;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String [] str = br.readLine().split(" ");
        int x = Integer.parseInt(str[0]);
        int y = Integer.parseInt(str[1]);
        Sub sub = new Sub(x, y);
        System.out.println(sub.sum());
    }
}
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 final int getY() {
        return y;
    }
    public int sum() {
        return getX() + getY();
    }
}
class Sub extends Base {
    public Sub(int x, int y) {
        super(x, y);
    }
    public final int sum() {
        return super.getX() * 10 + super.getY();
    }
}

Java 解法, 执行用时: 13ms, 内存消耗: 9644KB, 提交时间: 2022-05-20

import java.util.Scanner;
import java.io.*;
public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String [] str = br.readLine().split(" ");
        int x = Integer.parseInt(str[0]);
        int y = Integer.parseInt(str[1]);
        Sub sub = new Sub(x, y);
        System.out.println(sub.sum());
    }

}

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 final int getY() {
        return y;
    }

    public final int sum() {
        return getX() + getY();
    }

}

class Sub extends Base {

    public Sub(int x, int y) {
        super(x, y);
    }

    //write your code here......
    public int getX() {
        return 10 * super.getX();
    }

}

Java 解法, 执行用时: 14ms, 内存消耗: 9452KB, 提交时间: 2022-05-31

import java.util.Scanner;
import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String [] str = br.readLine().split(" ");
        int x = Integer.parseInt(str[0]);
        int y = Integer.parseInt(str[1]);
        Sub sub = new Sub(x, y);
        System.out.println(sub.sum());
    }

}

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 final int getY() {
        return y;
    }

    public final int sum() {
        return getX() + getY();
    }

}

class Sub extends Base {

    public Sub(int x, int y) {
        super(x, y);
    }

    //write your code here......
    public int getX() {
        return super.getX()*10;
    }

}

Java 解法, 执行用时: 14ms, 内存消耗: 9468KB, 提交时间: 2022-04-28

import java.util.Scanner;
import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String [] str = br.readLine().split(" ");
            int x = Integer.parseInt(str[0]);
            int y = Integer.parseInt(str[1]);
            Sub sub = new Sub(x, y);
            System.out.println(sub.sum());
        }
    }

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 final int getY() {
        return y;
    }

    public int sum() {
        return getX() + getY();
    }

}

class Sub extends Base {

    public Sub(int x, int y) {
        super(x, y);
    }

    @Override
   public final int sum(){
       return super.getX()*10+super.getY();
   }
    

}

Java 解法, 执行用时: 26ms, 内存消耗: 10696KB, 提交时间: 2022-02-09

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);
            System.out.println(sub.sum());
        }
    }

}

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 final int getY() {
        return y;
    }

    public  int sum() {
        return getX() + getY();
    }

}

class Sub extends Base {

    public Sub(int x, int y) {
        super(x, y);
    }

    //write your code here......
    public int sum(){
        return super.getX()*10 + super.getY();
    }

}

上一题