Điền phương thức phù hợp vào vị trí <> để ghi đè lên phương thức của lớp cha? public class Parent{ public void method(int x){…} } public class Child extends Parent{ <> } public void method(){…} public void method(int y){…} public void method(int x, String s){…} public void method(double x){…}
Question
Điền phương thức phù hợp vào vị trí <> để ghi đè lên phương thức của lớp cha? public class Parent{ public void method(int x){…} } public class Child extends Parent{ <> } public void method(){…} public void method(int y){…} public void method(int x, String s){…} public void method(double x){…}
Solution
Để ghi đè lên phương thức của lớp cha, bạn cần sử dụng từ khóa @Override trước phương thức bạn muốn ghi đè. Trong trường hợp này, phương thức bạn muốn ghi đè là phương thức "method(int x)" của lớp cha. Vì vậy, bạn cần điền @Override vào vị trí <> như sau:
public class Child extends Parent{
@Override
public void method(int x){
// code của bạn ở đây
}
public void method(){
// code của bạn ở đây
}
public void method(int y){
// code của bạn ở đây
}
public void method(int x, String s){
// code của bạn ở đây
}
public void method(double x){
// code của bạn ở đây
}
}
Lưu ý rằng, phương thức ghi đè phải có cùng tên, cùng tham số và cùng kiểu trả về với phương thức trong lớp cha.
Similar Questions
Cách định nghĩa các lớp đúng là: class Parent{final void method(){}} và class Child extends Parent{ void method(){}} class MyClass{final int PI = 3.14; void method(){PI = 3.1475;}} final class Parent{} và class Child extends Parent{} class Parent{void method(){}} và class Child extends Parent{ void method(){}}
Tìm lỗi sai về định nghĩa phương thức trong lớp sau: class MyClass { String method1() { return "A"; } void method() { } double method() { return 1.5; } method() { } } Select one:a. String method1(){return "A";}b. double method(){return 1.5;}c. void method(){}d. method(){}
Điền hàm tạo vào lớp Child để 2 lớp sau không phát sinh lỗi dịch? public class Parent{ public Parent(int x){} } public class Child extends Parent{ <> } public Child(){} public Child(){super(5);} public Child(int x){} public Child(){super();}
Giả sử 2 lớp MyClass1 và MyClass2 sau đây được khai báo trong cùng một package là com.poly. Hãy chọn câu lệnh phát sinh lỗi lúc dịch: package com.poly class MyClass1{ public void method1(){…} private void method2(){…} void method3(){…} } class MyClass2{ void method(){ MyClass1 o1 = new MyClass1(); o1.method1(); o1.method2(); o1.method3(); } } o1.method1(); o1.method2(); MyClass1 o1 = new MyClass1(); o1.method3();
Cho biết kết quả hiển thị của đoạn mã sau:class TestA{public void start(){System.out.println("TestA");}}class TestB extends TestA{public void start(){System.out.println("TestB");}public static void main(String[] args){((TestA)new TestB()).start();}}doneTestATestBLỗi biên dịchLỗi thực thi
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.