Polymorphic member characteristics

伊谢尔伦
Release: 2016-11-26 10:14:59
Original
868 people have browsed it

⒈Characteristics of non-static member functions in polymorphism:
①At compile time: See whether there is a calling method in the class to which the reference variable belongs. If there is, the compilation passes, otherwise the compilation fails;
② During runtime: See whether there is a method to be called in the class to which the object belongs.
------------------------------------------------- -
//In short: when a member function is called, look at the left side when compiling and the right side when running. //
-------------------------------------------------- ---
2. Characteristics of member variables with the same name in polymorphism: (ps: different names have not been verified)
Refer to the left side (the class to which the reference variable belongs) regardless of compilation or running
3. Static members in polymorphism Features of the function:
Regardless of compilation or running, refer to the left side (the class to which the reference variable belongs)
example:

Class Fu 
{ 
int num=1;
public void method1()
{
System.out.println("fu_1");
}
public void method3()
{
System.out.println("fu_3");
}
//静态方法(包括变量)不所属于对象,它绑定于所属的类,会在内存中提前加载出来
public static void method4()
{
System.out.println("fu_4");
}
}
Class Zi extends Fu
{
int num=2;
public void method1()
{
System.out.println("zi_1");
}
public void method2()
{
System.out.println("zi_2");
}
public static void method4()
{
System.out.println("zi_4");
}
}
Class Duotaitest
{
public static void main (String [] args)
{
Fu f=new Zi ();
f.method1();
//f.method2(); 此行代码若存在,则编译失败,Fu类中没此方法
f.method3();
f.method4();
System.out.println(f.num);
Zi z= new Zi();
System.out.println(z.num);
}
/* 
运行结果: zi_1
fu_3
fu_4 //静态方法不能被子类重写覆盖,若想调用子类中的静态方法(一般没这么用的,只是面试会用到)Zi.method4(类名.方法名)
1
2
*/
}
Copy after login



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template