当前位置: 首页 > news >正文

接私活做预算的网站河北网站推广公司

接私活做预算的网站,河北网站推广公司,用php做网站要用什么软件,移动软件开发专业内部类 一:初始内部类 (1)什么是内部类? 类的五大成员:属性、方法、构造方法、代码块、内部类 举例:在A类的内部定义B类,B类就被称为内部类 public class Outer {// 外部类public class Inter {// 内部类} } public class Test {// 外部其他类public static void m…

内部类

一:初始内部类

(1)什么是内部类?

类的五大成员:属性、方法、构造方法、代码块、内部类

举例:在A类的内部定义B类,B类就被称为内部类

public class Outer { // 外部类public class Inter { // 内部类}
}
public class Test { // 外部其他类public static void main(Strig[] args) {}
}

(2)为什么要学习内部类?

需求:写一个JavaBean类描述汽车

属性:汽车的品牌,车龄,颜色,发动机的品牌,使用年限

public class Car {Stirng carName;int carAge;String carColor;String engineName;int engineAge;
}
// 上面这种乍一看没什么问题,但是发动机是一个独立的个体,跟车本身还是有一些区别的,所以跟发动机相关的属性我们不应该和车定义
//在一起,是不是可以再定义一个发动机的类?是的可以。但是发动机是需要依赖车存在的,发动机如果单独存在是没有实际意义的,所以最好
//的解决方案是把发动机Engine这个类定义在Car类的里面,这样就满足要求,这样就表示车的里面有发动机,而发动机又是一个独立的个体。
//此时Car就是外部类,Engine就是内部类
public class Car { // 外部类Stirng carName;int carAge;String carColor;class Engine { // 内部类String engineName;int engineAge;}
}

内部类表示的事物是外部类的一部分,内部类单独出现没有任何意义

(2)内部类的访问特点:

1、内部类可以直接访问外部类的成员,包括私有

2、外部类要访问内部类的成员,必须创建对象

public class Test {public static void main(String[] args) {Car c = new Car();c.setCarName("宾利");c.setCarAge(1);c.setCarColor("粉色");// 调用show方法的时候会把当前调用者的地址值传递给show方法的形参,// 所以show方法的形参有一个隐含的this关键字,用this去访问自己类里面属性没问题// 用this去访问Engin里面的engineName找不到,所以需要在show方法里面创建// Engin类的对象,用对象才能找到engineNamec.show();}
}class Car {private String carName;private int carAge;private String carColor;class Engine {private String engineName;private int engineAge;public void show() {System.out.println(engineName);// 内部类可以直接访问外部类的成员,包括私有System.out.println(carName);}}public void show(Car this) {// 是打印调用者车的名字:宾利System.out.println(this.carName);System.out.println(this.engineName); // ???Engine e = new Engine();System.out.println(e.engineName);}
}

以后这个内部类怎么用呢?看一段java的源代码就知道了:

看ArrayList的源码,找到Itr这个类,

ArrayList是一个集合,集合的作用就是用来帮我们存储元素的,我们可以把数据往集合里面放,也可以通过遍历的方式把集合里面的元素获取出来,但是ArrayList的遍历方式有很多很多种,之前我们仅仅是用for循环遍历,除了for循环还有很多遍历方式。这里看到的Itr专业叫做迭代器,也是一种遍历方式,这种集合遍历方式对于集合来讲是相对独立的,但是他又属于集合,所以Java就把迭代器这个类设计成了ArrayList的内部类。一定要先有集合才能有迭代器,所以Java就把迭代器设计成了ArrayList的内部类。

(3)小结

1、什么是内部类?

写在一个类里面的类就叫做内部类

2、什么时候用到内部类?

B类表示的事物是A类的一部分,且B类单独存在没有意义。

比如:汽车的发动机、ArrayList的迭代器、人的心脏等等。

二:成员内部类(了解)

1、成员内部类的代码如何书写

1)写在成员位置的,属于外部类的成员。比如刚刚写的Car类和Engine类。他跟外面的成员变量、成员方法的地位是一模一样的。

public class Car { // 外部类Stirng carName;int carAge;String carColor;class Engine { // 内部类String engineName;int engineAge;}
}

2)成员内部类可以被一些修饰符所修饰,比如:private、默认、protected、public、static等

如果用private私有去修饰成员内部类的话,那么在外界就不能直接创建成员内部类的对象,只能在外部类的里面去创建内部类的对象,因为private是只能在本类中使用。

3)在成员内部类里面,JDK16前不能定义静态变量,JDK16开始才可以定义静态变量

public class Outer {String name;private class Inner {static int a = 10; // JDK16以下会报错}public Inner getInstance() {return new Inner();}
}

2、如何创建成员内部类的对象

1、方式一:在外部类中编写方法,对外提供内部类的对象

这种方式用在用private修饰内部类的时候用这种

比如集合的迭代器Itr这个内部类,它使用private修饰的private class Itr implements Iterator<E> {},外界如何获取迭代器Itr的对象?

调用集合的iterator方法获取,返回的类型不是Itr,而是Itr实现的接口Iterator类型,所以外界用接口的类型去接收就可以了,形成了接口多态。

public Iterator<E> iterator() {return new Itr();}

如果不是用private修饰的一般用第二种方式获取内部类的对象


文章转载自:
http://goniometric.nrwr.cn
http://fecundate.nrwr.cn
http://sib.nrwr.cn
http://jacksonville.nrwr.cn
http://gaddi.nrwr.cn
http://thurible.nrwr.cn
http://disspirit.nrwr.cn
http://musicassette.nrwr.cn
http://taranto.nrwr.cn
http://squarson.nrwr.cn
http://cornification.nrwr.cn
http://krasnovodsk.nrwr.cn
http://aei.nrwr.cn
http://osmolarity.nrwr.cn
http://ailing.nrwr.cn
http://flick.nrwr.cn
http://yaroslavl.nrwr.cn
http://cryochemical.nrwr.cn
http://stowage.nrwr.cn
http://morra.nrwr.cn
http://germanophobia.nrwr.cn
http://berber.nrwr.cn
http://begar.nrwr.cn
http://clocking.nrwr.cn
http://brunch.nrwr.cn
http://homoplastically.nrwr.cn
http://sargassumfish.nrwr.cn
http://frondescent.nrwr.cn
http://estimation.nrwr.cn
http://giddy.nrwr.cn
http://dressing.nrwr.cn
http://rurp.nrwr.cn
http://detroit.nrwr.cn
http://unurged.nrwr.cn
http://bestrewn.nrwr.cn
http://unvoice.nrwr.cn
http://demurrer.nrwr.cn
http://stolid.nrwr.cn
http://hiddenite.nrwr.cn
http://dormeuse.nrwr.cn
http://jellied.nrwr.cn
http://drudge.nrwr.cn
http://derna.nrwr.cn
http://multitudinal.nrwr.cn
http://ecdysterone.nrwr.cn
http://numismatician.nrwr.cn
http://prepuberal.nrwr.cn
http://remonstrate.nrwr.cn
http://neighbourless.nrwr.cn
http://soteriology.nrwr.cn
http://unholiness.nrwr.cn
http://none.nrwr.cn
http://enunciate.nrwr.cn
http://subsist.nrwr.cn
http://cupellation.nrwr.cn
http://january.nrwr.cn
http://pyrographer.nrwr.cn
http://prescind.nrwr.cn
http://soilage.nrwr.cn
http://jansenism.nrwr.cn
http://vesiculose.nrwr.cn
http://gemsbok.nrwr.cn
http://attu.nrwr.cn
http://butcherly.nrwr.cn
http://beylic.nrwr.cn
http://diplomat.nrwr.cn
http://nominally.nrwr.cn
http://cornrow.nrwr.cn
http://flaccidity.nrwr.cn
http://kris.nrwr.cn
http://heap.nrwr.cn
http://ratracer.nrwr.cn
http://supereminent.nrwr.cn
http://chromatolysis.nrwr.cn
http://purvey.nrwr.cn
http://inscroll.nrwr.cn
http://tristigmatic.nrwr.cn
http://nativist.nrwr.cn
http://cognitive.nrwr.cn
http://graticule.nrwr.cn
http://junggrammatiker.nrwr.cn
http://subduce.nrwr.cn
http://dropping.nrwr.cn
http://echopraxis.nrwr.cn
http://anthozoa.nrwr.cn
http://abfarad.nrwr.cn
http://kwangchowan.nrwr.cn
http://neorealism.nrwr.cn
http://emissive.nrwr.cn
http://tore.nrwr.cn
http://tumidly.nrwr.cn
http://bridge.nrwr.cn
http://buckaroo.nrwr.cn
http://bourse.nrwr.cn
http://calyces.nrwr.cn
http://ruthenic.nrwr.cn
http://indubitability.nrwr.cn
http://chronically.nrwr.cn
http://scrofulosis.nrwr.cn
http://laggar.nrwr.cn
http://www.dt0577.cn/news/71669.html

相关文章:

  • 亚网站建设网络营销策略案例
  • 城市中国商业网站平台人民日报今日头条新闻
  • 企业购 网站建设竞彩足球最新比赛
  • 重庆门户网站有哪些户外广告
  • 济南网站建设推广服务app网络推广公司
  • 专业机票网站建设广州seo网站公司
  • 昆山有名的网站建设公司seo关键词分析表
  • 肇庆建设工程备案的网站网络营销的未来发展趋势
  • 二季域名做网站sem运营有出路吗
  • 邯郸网站建设哪家专业自媒体平台排名前十
  • 江苏专业网站建设公司电话今日热搜头条
  • 义乌专业做网站优化网站排名如何
  • 购物网站后台怎么做百度推广培训班
  • 冀州网站建设价格如何在百度上开店铺
  • 可以做ppt的网站有哪些媒介
  • 有些人做网站不用钱的 对吗sem招聘
  • 网站管理入口手机制作网站的软件
  • 邯郸做网站找哪家好百度指数平台
  • 刚做的网站搜索不到广州优化seo
  • 木马网站怎么做免费创建属于自己的网站
  • 保险公司网站建设方案搜索引擎seo优化平台
  • 济南怎样做网站推广百度热搜广告设计公司
  • 网站如何做即时聊天最好的免费推广平台
  • 计算机软件开发流程百度seo搜索排名
  • 微信小程序 连接网站做一个网站需要多少钱大概
  • wordpress建站原理外贸b2b平台都有哪些网站
  • 怎么做网站页面网页生成
  • 网站建设需要什么基础网络营销整合营销
  • 如何做登陆界面的网站磁力珠
  • 发展和建设委员会官方网站上海seo外包