Knowee
Questions
Features
Study Tools

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

Question

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

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Câu lệnh phát sinh lỗi lúc dịch trong đoạn mã trên là:

o1.method2();

Lý do là vì method2() trong lớp MyClass1 được khai báo với phạm vi truy cập là private, do đó nó chỉ có thể được truy cập từ bên trong lớp MyClass1. Khi cố gắng gọi method2() từ lớp MyClass2, trình biên dịch sẽ báo lỗi truy cập.

Similar Questions

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 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){…}

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

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

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

1/3

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.