`
danielhjd
  • 浏览: 242876 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

(笔记)关于java中的一些问题

    博客分类:
  • Java
阅读更多

第一题:

package william;

abstract class Name {
	private String name;
	public abstract boolean inStupidName(String name){
		System.out.println("okay");
	};
}

 

报错:



 问题:1) Abstract methods do not specify a body;

          2) This method must return a result of type boolean;

          3) The field Name.name is never read locally;

 

第二题:

package william;

public class Outer {
	private String str="this is outter";
		class Inner{
		String str="this is inner";
		void inner_f1(){
			System.out.println(this.str);
			System.out.println(new Outer().str);

		}
	}
	

	
	public static void main(String[] args){
		Outer out=new Outer();
		Outer.Inner inner=out.new Inner();
		inner.inner_f1();
	}
}

 

注意问题:

1:Outer out=new Outer(); Outer.Inner inner=out.new Inner(); 实例化一个内部类;

     No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclosing instance of type Outer (e.g. x.new A() where x is an instance of Outer).

2:System.out.println(this.str);  System.out.println(new Outer().str);

 

第三题:

public class C extends B implements A {
	public void pX(){
		System.out.println(super.str);
		System.out.println(A.str);
	}
	public static void main(String[] args) {
		new C().pX();

	}

}

注意问题:

1:调用父类,用super;

2:调用接口,不需要instantiate;The static field A.str should be accessed in a static way;

 

第四题:

class A;

package test;

 class A {
	String s;

 A(String s) {
		this.s=s;
	}
	public void print(){
		System.out.println(s);
	}
}

 

Class Testt;




 

报错:

1)Implicit(不明确的) super constructor A() is undefined for default constructor. Must  define an explicit (明确的)constructor;

2)如果删除A类中的构造方法,这个程序可以通过编译但是由于S没有赋值输出的结果为null;

 

修改:

package test;

public class Testt extends A {

	Testt(String s) {
		super(s);
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		Testt t=new Testt("s");
		t.print();
	}

}

 

注意:

1)此处是含参数的构造器constructor;

 

第五题;

package test;

public class TestB extends TestA {
	public void start(){
		System.out.println("TestB");
	}
	public static void main(String[] args) {
	((TestA)new TestB()).start();
	
	}

}

 无报错,可以通过编译;

输出结果为:

TestB

1)  override
 

 第六题:有关继承的关系;

  TestA

package test;

class TestA {
	String test="Hello TestA";
	public void start(){		
		System.out.println("TestA");
	}
}

 TestB

package test;

public class TestB extends TestA {
	String test="Hello testB";
	public void start(){
		System.out.println(super.test);
		System.out.println("TestB");
		System.out.println(test);
	}
	public static void main(String[] args) {
		 TestA t= new TestB();
	     t.start();
	     System.out.println(t.test);
	
	}

}

 

输结果:

Hello TestA
TestB
Hello testB
Hello TestA

 

1)在主函数中,如何调用父类调用父类的属性和方法;

	public static void main(String[] args) {
		 TestA t= new TestA();
	     t.start();
	     System.out.println(t.test);
	
	}

输出结果:

TestA
Hello TestA

 

 2)在主函数中,如何通过子类调用父类的属性,同时又可以调用子类的属性和方法;

	public static void main(String[] args) {
		 TestA t= new TestB();
	     t.start();
	     System.out.println(t.test);
	
	}

 输出结果:

Hello TestA
TestB
Hello testB
Hello TestA

 

3)在主函数中,如果子类overide父类的方法,那么父类的方法还可以被子类调用吗?

在2)中我们用父类实例了一个对象t,结构调用start()的方法时,却是调用子类的方法。

Parent;

package test;

class Parent {
	public Parent() {
	   System.out.println("Parent_constructor");
	   test();
	}

	public void test() {
	   System.out.println("Parent_test");
	}
	}

 Child;

package test;

public class Child extends Parent {
	private String name = "张三丰";
	public Child() {
		super();
		this.test();
	   System.out.println("Child_constructor");

	}

	public void test() {
	   System.out.println("Child_test,name=" + name);
	}

	public static void main(String[] args) {
    new Child();   
	}
	}

输出结果;

Parent_constructor
Child_test,name=null
Child_test,name=张三丰
Child_constructor

总结:

1:当父类不含有construtor;

     a.子类没有override父类的方法

        子类实例化对象可以直接调用父类的方法和属性;如果属性父类和子类有相同的属性,在调用父类的属性时,通过Parent p=new Child();可以获取parent的属性...

package test;

class Parent {

	public void test1() {
	   System.out.println("Parent_test");
	}
	}

 

package test;

public class Child extends Parent {
	private String name = "张三丰";
	public void test() {
	   System.out.println("Child_test,name=" + name);
	}

	public static void main(String[] args) {
   Child t=new Child();  
   t.test();
   t.test1();
   System.out.println(t.name);
    
	}
	}

 

      如果属性父类和子类有相同的属性,在调用父类的属性时,通过Parent p=new Child();可以获取parent的属性...

package test;

class Parent {
	String name="张二疯";
	
	public void test1() {
	   System.out.println("Parent_test");
	}
	}

 

package test;

public class Child extends Parent {
	private String name = "张三丰";
	public void test() {
	   System.out.println("Child_test,name=" + name);
	}

	public static void main(String[] args) {
		Child c=new Child();
		Parent p=new Child();  
		p.test1();
		System.out.println(c.name);
		System.out.println(p.name);
    
	}
	}

  

     b.子类overrride父类的方法

        无论是通过子类实例化 还是父类通过子类实例化,因为父类的方法被子类override所以输出的结果都是一样;

 

package test;

class Parent {
	String name="张二疯";
	
	public void test() {
	   System.out.println("Parent_test");
	}
	}

 

package test;

public class Child extends Parent {
	private String name = "张三丰";
	public void test() {
	   System.out.println("Child_test,name=" + name);
	}

	public static void main(String[] args) {
		Child c=new Child();
		Parent p=new Child();  
		c.test();
		p.test();
		System.out.println(c.name);
		System.out.println(p.name);
    
	}
	}

 

输出:

Child_test,name=张三丰
Child_test,name=张三丰
张三丰
张二疯

 

2:当父类含有不含参数的construtor;

     子类的默认的construtor;继承了父类的construtor;

package test;

class Parent {
	public Parent() {
	   System.out.println("Parent_constructor");
	   test();
	}
	
	public void test() {
	   System.out.println("Parent_test");
	}
	}

 

 

package test;

public class Child extends Parent {
	private String name = "张三丰";
	public void test() {
	   System.out.println("Child_test,name=" + name);
	}

	public static void main(String[] args) {
		new Child();
	}
	}

 

package test;

public class Child extends Parent {
	private String name = "张三丰";

	public Child() {
		super();
	}

	public void test() {
	   System.out.println("Child_test,name=" + name);
	}

	public static void main(String[] args) {
		new Child();
	}
	}

  

输出结果:

Parent_constructor
Child_test,name=null

2:当父类含有含参数的construtor;

    在子类中必须声明一个含参数的construtor,并super()继承,如第四题。

第七题:

父类:

  class ExamSuperA {
	int m = 0;
	int n = 0;
	long f(){
		return m+n;
	}
}

 

子类:

class B extends ExamSuperA{
		int m=1,n=1;
		long f(){
			long result = 0;
			super.m=10;
			super.n=20;
			result = super.f()+(m+n);
			return result;
		}
	long g(){
		long result = 0;
		result = super.f()+(m+n);
		System.out.println(super.m);
		return result/2;
	}
}
public class ExamSuper {
	public static void main(String[] args) {
		B b = new B();
		b.m =3;
		b.n = 7;
		long resultOne = b.g();
		long resultTwo = b.f();
		long resultThree = b.g();

		System.out.println("resultOne="+resultOne);
		System.out.println("resultTwo="+resultTwo);
		System.out.println("resultThree="+resultThree);
	}

}

 

输出结果:

0

10

resultOne=5

resultTwo=40

resultThree=20

 

总结:

通过子类实例化 b  

执行long resultOne = b.g(); super.m=0 所以输出的第一个结果为“0”;

执行long resultTwo = b.f();由于子类override父类的,所以此时调用的f()方法是子类的,子类f()方法中又将super.m;和super.n重新赋值;

执行long resultOne = b.g();其中调用的Super.f()中m.n属性的值已经通过子类修改,所以super.f()的值也发生了改变...

特别注意:super.f()中的属性被修改了 区别于:为什么在Operate这个方法中运行后,X的值改变,但Y没有?

 

 

 

Child c=new Child();

Parent p=new Child();

对象c不仅有自己属性和方法,还继承了父类的方法和属性;

如果子类将父类的方法override了,那么用对象c调用的方法为Child 类的方法;p的方法也将被子类的方法override了,也就是说p的方法被override了,原来的方法不存在了。

如果子类将父类的属性同名,可以通过p来调用parent的同名属性。

 

  • 大小: 24.8 KB
  • 大小: 25.8 KB
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics