1、访问修饰符access modifier:
1)public 修饰的方法和属性可被所有类访问
2)protected 可在类内部、相同包、该类的子类所访问
3)private 只能在该类内部使用
4)默认的:在类内部及相同包下面的类使用
例一:相同包内
package com.shengsiyuan;
public class C
{
protected int year = 2016;
}
package com.shengsiyuan;
public class D
{
public static void main(String[] args)
{
C c = new C();
System.out.println(c.year);
}
}
例二:该类的子类访问
package com.shengsiyuan;
public class C
{
protected int year = 2016;
}
package com.shengsiyuan.test;
import com.shengsiyuan.C;
public class D extends C
{
public void output()
{
System.out.println(year);
}
public static void main(String[] args)
{
D d = new D();
d.output();
}
}
2、instanceof:判断某个对象是否是某个类的实例。
public class InstanceofTest
{
public static void main(String[] args)
{
People people = new People();
System.out.println(people instanceof People);
}
}
class People
{
}
public class InstanceofTest
{
public static void main(String[] args)
{
People people = new Man();
System.out.println(people instanceof People);//结果为true.
}
}
class People
{
}
class Man extends People
{
}