public class PrivateOverride { private void f() { System.out.println("private f()"); } public static void main(String[] args) { PrivateOverride po = new Derived(); po.f(); } } class Derived extends PrivateOverride { public void f() { System.out.println("public f()"); } }
时间: 2024-03-31 07:35:48 浏览: 123
该程序会编译错误,因为在 Derived 类中尝试重写 PrivateOverride 类中的私有方法 f(),但是私有方法是不可以被重写的。如果将 PrivateOverride 类中的 f() 方法改为 protected 或 public,那么程序会输出 "private f()",因为在 main 方法中,创建了一个 Derived 对象并赋值给了 PrivateOverride 类型的变量 po,然后调用了 po 的 f 方法。由于 f 方法是 PrivateOverride 类中的私有方法,所以编译器会在编译时静态绑定调用 PrivateOverride 类中的 f 方法,输出 "private f()"。这就是 Java 中的方法重载和静态绑定。
相关问题
class A { private void f(){ System.out.println("A: f"); } } class B extends A { public void f(){ System.out.println("B: f"); } public static void main(String args[]){ B b=new B(); B.f(); } }
这段代码有问题。在类B中,public void f() 与类A中的private void f()并不是重写关系,因为它们的访问修饰符不同,所以类B中的public void f()实际上是一个新的方法,与类A中的方法没有关系。在B.f()中,应该调用对象方法,而不是类方法,所以应该改为 b.f()。改正后的代码如下:
```
class A {
private void f() {
System.out.println("A: f");
}
}
class B extends A {
public void f() {
System.out.println("B: f");
}
public static void main(String args[]) {
B b = new B();
b.f();
}
}
```
class Person{ private String name; private int age; private char gender; public Person(String name,int age,char gender){ this.name=name; this.age=age; this.gender=gender; } public void printInfo(){ System.out.println("姓名: "+name); System.out.println("年龄: "+age); System.out.println("性别: "+gender); } } class Student extends Person{ private String school; private int score; public Student(String name,int age,char gender,String school,int score){ super(name,age,gender); this.school=school; this.score=score; } public void printInfo(){ super.printInfo(); System.out.println("学校: "+school); System.out.println("成绩: "+score); } } class Teacher extends Person{ private String course; public Teacher(String name,int age,char gender,String course){ super(name,age,gender); this.course=course; } public void printInfo(){ super.printInfo(); System.out.println("课程: "+course); } } //替换下面的学生信息,修改主类的名字 public class Experiment_04_01 { public static void main(String[] args) { Student stud=new Student01("lili",19,'F',"齐鲁工业大学",90); Teacher tech=new Teacher("coco",30,'F',"高级程序设计"); stud.printInfo(); System.out.println(); tech.printInfo(); } }
抱歉,您的代码有一些错误。首先,您在创建学生对象时使用了错误的类名,应该是"Student"而不是"Student01"。其次,由于您的代码中没有定义名为"Student01"的类,因此需要将主类的名字从"Experiment_04_01"修改为"Main"。以下是修改后的代码:
```java
class Person {
private String name;
private int age;
private char gender;
public Person(String name, int age, char gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public void printInfo() {
System.out.println("姓名: " + name);
System.out.println("年龄: " + age);
System.out.println("性别: " + gender);
}
}
class Student extends Person {
private String school;
private int score;
public Student(String name, int age, char gender, String school, int score) {
super(name, age, gender);
this.school = school;
this.score = score;
}
public void printInfo() {
super.printInfo();
System.out.println("学校: " + school);
System.out.println("成绩: " + score);
}
}
class Teacher extends Person {
private String course;
public Teacher(String name, int age, char gender, String course) {
super(name, age, gender);
this.course = course;
}
public void printInfo() {
super.printInfo();
System.out.println("课程: " + course);
}
}
public class Main {
public static void main(String[] args) {
Student stud = new Student("lili", 19, 'F', "齐鲁工业大学", 90);
Teacher tech = new Teacher("coco", 30, 'F', "高级程序设计");
stud.printInfo();
System.out.println();
tech.printInfo();
}
}
```
请注意,我已经将主类名更改为"Main",并将学生对象的类名更正为"Student"。现在您可以运行该代码并得到预期的输出。
阅读全文