public class Test {
public static void main(String[] args){
People p1=new Student();
System.out.print(p1);
p1.speak();
}
}
abstract class People{
public String sex;
public abstract void speak();
}
class Student extends People{
private String sex;
private void speak(){
System.out.println(" speak Chinese");
}
public String toString(){ //we can override this function both in class People and class Student
return "I";
}
}
运行结果:Cannot reduce the visibility of the inherited method from People
这是因为如果子类要覆盖了父类中定义的方法,那么不能降低其可见性。正如上面的例子,人可以讲话,但学生作为人的一种却不能讲话,这显然是不合理的。即如果在父类中定义一个protected方法,那么在子类中可以将其覆盖,并将访问控制属性改为public,反过来则不行。不过数据域则没有该限制,上例中People类中定义了public String sex;而在子类中定义private String sex;这是可行的,该属性是各自类的成员变量,不存在覆盖的问题。另外,该例中还涉及到了覆盖Object类的toString方法,如果不在子类中重写该方法,那么System.out.print(p1);将会输出类似 speak Chinese的一个字符串(假设前面所提到的错误已改正),这个字符串的信息不是很有用,应该在子类中加以覆盖,使它返回一个代表该对象的易懂的字符串。覆盖定义的toString()方法既可以写在People类里,也可以写在Student类里,如果两个类中都重写了,最后调用的将是Student类中的toString().
改正了上述错误后,输出结果为:I speak Chinese
相关推荐
It is found that the dehazed image suffers from little noise and the details of the objects close to the observer can be preserved well. In addition, this algorithm is also proved to be useful for ...
Our method minimizes the number of issued queries and reduces the delays due to the latency of query results. We reuse the results of occlusion queries from the last frame in order to initiate and ...
It resolves visibility among an arbitrary collection of opaque, transparent, and intersecting objects. Using an easy to compute Fourier window (box flter), it increases the effective image resolution...
depicts the visibility graph motifs in 4 nodes
tinting lms from the gray market and pastes the same on the glass windows, which in turn hamper the visibility, thereby preventing the law enforcement authorities from having a clear view of those ...
environment, and the adverse weather conditions such as precipitation, ice, and low visibility seriously affect the safety operation of airport traffic. Therefore, it is necessary to study the impact ...
However, the method's visibility is different, which is allowed in Java. - **Option B**: `protected void setVar(int a, int b, float c){}` - This option does not overload the `setVar` method because ...
Based on the provided information from the book "C# 3.0 With the .NET Framework 3.5 Unleashed," we can extract several key points and concepts that are essential for understanding the fundamentals of ...
The design presented here is based on the input of numerous professionals from the pointing device manufacturing and Windows soft¬ware development industries. In this document, the words "tablet" ...
video and interactive 3D material, which not only improve the perception of information through greater visibility, but and greatly improve the quality of the generated documentation. Using ...
- **When to Produce**: Production is triggered by the withdrawal of items from downstream processes. - **What to Replenish**: Decisions are made based on what has been consumed. - **When to Pull**: ...
标题“Fast Visibility Restoration from a Single Color or Gray Level Image”指的是一个图像处理技术,该技术旨在快速恢复图像中的可见性,特别是在有雾或低对比度的情况下。这个过程通常被称为去雾或者图像去...
•Q581933 - At design time, modifying any property of a control selected on the parent form selects a control on an inherited form if the parent form contains TdxWizardControl and/or ...
【船级社】 NK Rules for the Survey and Construction of Steel Ships _ Guidance Part W NAVIGATION BRIDGE VISIBILITY.pdf
VISIBILITY属性详解 在 Android 开发中,控件的可见性是非常重要的,VISIBILITY 属性就是控制控件的显示和隐藏的。通过设置 VISIBILITY 属性,我们可以控制控件的可见性,达到不同的显示效果。 VISIBLE、INVISIBLE...
8. **Surface Treatment**: The surface treatment is anti-glare, which helps reduce reflections and improve visibility in bright lighting conditions. 9. **Color Arrangement**: The color arrangement is ...
### 基于单张图像的恶劣天气下能见度增强技术 #### 概述 在计算机视觉领域,恶劣天气条件如雾、霾等对场景可见性的显著降解是一个重大挑战。这种现象从光学角度来看,主要是由于大气中存在的大量粒子吸收和散射...