class Parent{ public Parent(){ System.out.println("1"); } } public class Child extends Parent{ public Child(){ System.out.println("2"); } public static void main(String[] args) { new Child(); } }
class Parent{ public Parent(){ public void test(){ System.out.println("1"); } } }public class Child extends Parent{ public Child(){ //覆盖上层方法 public void test(){ System.out.println("2"); } } } test.java: public static void main(String[] args) { Parent a = new Parent(); Child b = new Child(); a.test(); b.test();}
//父类 class Parent{ public Parent(){ private void test(){ System.out.println("1"); } } } //子类继承父类 class Child extends Parent{ public Child(){ //覆盖上层方法 private void test(){ System.out.println("2"); } } } public class test{ public static void main(String[] args) { Parent a = new Parent(); Child b = new Child(); a.test(); b.test();} }
public class Test { public static void main(String[] args) { Parent p1 = new Parent(); Parent p2 = new Child(); p1.test(); p2.test(); } }class Parent { public void test() { System.out.println("My name is Parent."); } }class Child extends Parent { public void test() { System.out.println("My name is Child."); } }
class Parent { Parent(){} void show(){ System.out.println("1"); } } class Child extends Parent {Child(){} void disp(){ System.out.println("2");} } class ParentTest{ public static void main(String[] args) { Parent a = new Parent(); Child b = new Child(); a.show(); b.disp(); } }
public Parent(){
System.out.println("1");
}
}
public class Child extends Parent{
public Child(){
System.out.println("2");
}
public static void main(String[] args) {
new Child();
}
}
public Parent(){
public void test(){
System.out.println("1");
}
}
}public class Child extends Parent{
public Child(){
//覆盖上层方法
public void test(){
System.out.println("2");
}
}
}
test.java:
public static void main(String[] args) {
Parent a = new Parent();
Child b = new Child(); a.test();
b.test();}
class Parent{
public Parent(){
private void test(){
System.out.println("1");
}
}
}
//子类继承父类
class Child extends Parent{
public Child(){
//覆盖上层方法
private void test(){
System.out.println("2");
}
}
}
public class test{
public static void main(String[] args) {
Parent a = new Parent();
Child b = new Child(); a.test();
b.test();}
}
public static void main(String[] args) {
Parent p1 = new Parent();
Parent p2 = new Child();
p1.test();
p2.test();
}
}class Parent {
public void test() {
System.out.println("My name is Parent.");
}
}class Child extends Parent {
public void test() {
System.out.println("My name is Child.");
}
}
象上面这个javac Test.java
报好多错误
{
Parent(){}
void show(){
System.out.println("1");
}
}
class Child extends Parent
{Child(){}
void disp(){
System.out.println("2");}
}
class ParentTest{
public static void main(String[] args)
{
Parent a = new Parent();
Child b = new Child(); a.show();
b.disp(); }
}
我javac 后
但是不能java运行