Instanceof关键字
来源:互联网 发布:国泰君安投资顾问 知乎 编辑:程序博客网 时间:2024/06/08 06:52
一、 Instanceof关键字
1、 该语句一般判断某个对象是否为某个类的实例,是返回true,不是返回false,
2、 语法:对象 instanceof 类型,返回布尔型
二、 父类设计发则
1、 父类一般都设计为抽象类或者接口,其中优先考虑接口,如果接口不满足才考虑抽象类
2、 一个具体的类尽可能的不去继承另外一个具体的类,这样的好处就是无需检查是否为父类的对象
代码如下:
/**
instanceof关键字
*/
class IntanceofKeyDemo
{
public static void main(String[] args)
{
Man man = new Man();
say(man);
Woman woman = new Woman();
say(woman);
} public static void say(Person p)//本方法来个功能第一:输出所有的男女说的话 //第二:如果是女生对象则输出生气是说的话 { p.say(); if(p instanceof Woman) { Woman w = (Woman)p; w.getAngry(); } } /*public static void say(Man m) { m.say(); } public static void say(Woman w) { w.say(); } */
}
abstract class Person
{
private String name;
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public abstract void say();//抽象方法
}
class Man extends Person
{
public void say()
{
System.out.println(“人家是纯爷们”);
}
}
class Woman extends Person
{
public void say()
{
System.out.println(“人家是女生哦”);
}
//本类中进行扩展的方法
public void getAngry()
{
System.out.println(“讨厌了,人家生气了啦!我去,真矫情!”);
}
}
- instanceof关键字
- Instanceof关键字
- instanceof关键字
- instanceof关键字
- instanceof关键字
- instanceof关键字
- instanceof关键字
- instanceof关键字
- instanceof关键字
- instanceof关键字
- instanceof 关键字
- instanceof关键字
- Instanceof关键字
- instanceof关键字
- instanceof关键字
- instanceof 关键字
- instanceof关键字
- instanceof关键字
- 查看VS2013中项目的宏定义
- android Preference之android:dependency
- Retrofit2打印 网络请求日志
- centos6.5启动报错处理
- 设置webview禁用长按弹出框
- Instanceof关键字
- 找出缺失的整数
- eclipse在ubuntu16.04无法设置以及新建工程卡死解决方案
- tableView 的一些常用属性,方法
- nullptr,constexpr和constexpr函数
- 二叉查找树的创建和遍历
- jsp中添加表达式
- 千里之行始于足下
- 简谈I2C总线