Java面向对象中的继承

Java基础[03-面向对象]. 面向对象是相对面向过程而言 面向对象和面向过程都是一种思想 面向过程 强调的是功能行为 面向对象 将功能封装进对象,强调具备了功能的对象。 面向对象是基于面向过程的。 立即下载

面向对象特征----继承

继承概念

package com.cloud.day4;

/*

在现实生活中事物与事物之间是存在关系.

   球员---->球队  整体与部分关系   has a 关系 

   学生----->人    继承的关系      is a 关系

*/

class Player{

   int num;

   String name;

   public Player(int num,String name){

      this.num = num;

      this.name = name;

   }

   public void run(){

      System.out.println(name+"running....");

   }

}

class Team{

   String name;

   Player p1;

   Player p2;

   Player p3;

   public Team(String name,Player p1,Player p2,Player p3){

      this.name=name;

      this.p1=p1;

      this.p2=p2;

      this.p3=p3;

   }

   public void satartGame(){

      System.out.println(name+"开始比赛...");

   }

}

public class Demo5 {

   public static void main(String[] args) {

      Player p1 = new Player(12,"梅西");

      Player p2 = new Player(7,"C罗");

      Player p3 = new Player(11,"内马尔");

      //球队

      Team t = new Team("恒大",p1,p2,p3);

      t.satartGame();

     

      System.out.println("名字:"+ t.p2.name);

   }

}

继承注意事项

package com.cloud.day4;

/*

目前存在的问题:

   1. 无法描述清楚这两个类之间的继承关系。

   2. 存在着重复代码。

面向对象的三大特征:

   1. 封装

   2. 继承

   3. 多态.

继承:继承是通过关键字extends体现的。

继承的格式:

   class 类名1 extends 类名2{

   }

继承要注意的事项:

   1. 千万不要为了减少重复代码而去继承,只有真正存在着继承关系的时候才去继承。

   2. 父类私有的成员不能被继承。

   3. 父类的构造函数不能被继承。

   4. 创建子类对象时默认会先调用父类无参的构造函数。

*/

class Person{

   String name;

   private int age;

   public Person(String name){

      this.name = name;

   }

   public Person(){

      System.out.println("调用了Person的构造方法");

   }

   public void eat(){

      System.out.println(name+"吃饭...");

   }

}

class Child extends Person{

   int num;

   public Child(){

      System.out.println("调用了Student的构造方法");

   }

   public void study(){

      System.out.println(name+"好好学习");

   }

}

public class Demo6 {

   public static void main(String[] args) {

      Child c = new Child();

      c.name="summer";

      c.study();

   }

}

构造方法在继承中的使用

package com.cloud.day4;

/*

调用父类构造方法的意义

初始化从父类继承下的属性

 */

class Fu{

   int x = 10;

   String name;

   public Fu(String name){

      this.name = name;

      System.out.println("父类带参数的构造方法...");

   }

   public Fu(){

      System.out.println("父类无参数的构造方法...");

   }

}

class Zi extends Fu{

   int x = 20;

   public Zi(String name){

      //指定调用父类带参数的构造函数

      super(name);

   }

   public void print(){

      System.out.println(x);

   }

}

public class Demo7 {

   public static void main(String[] args) {

      Zi z = new Zi("we");

      System.out.println(z.name);

      z.print();

   }

}

Super关键字

package com.cloud.day4;

/*

super关键字:

super关键字代表了父类空间的引用。

super关键字的作用:

   1. 子父类存在着同名的成员时,在子类中默认是访问子类的成员,可以通过super关键字指定访问父类的成员。

   2. 创建子类对象时,默认会先调用父类无参的构造方法,可以通过super关键字指定调用父类的构造方法。


super关键字调用父类构造方法要注意的事项:

   1. 如果在子类的构造方法上没有指定调用父类的构造方法,那么java编译器会在子类的构造方法上面加上super()语句。

   2. super关键字调用父类的构造函数时,该语句必须要是子类构造函数中的第一个语句。

   3. super与this关键字不能同时出现在同一个构造函数中调用其他的构造函数。因为两个语句都需要第一个语句。

super关键字与this关键字的区别:

   1. 代表的事物不一致。

        1. super关键字代表的是父类空间的引用。 

        2. this关键字代表的是所属函数的调用者对象。

   2. 使用前提不一致。

        1. super关键字必须要有继承关系才能使用。

        2. this关键字不需要存在继承关系也可使用。

   3. 调用构造函数的区别:

        1. super关键字是调用父类的构造函数。

        2. this关键字是调用本类的构造函数。

*/

class Fu1{

   int x=10;

   String name ;

   public Fu1(){

      System.out.println("父类无参构造方法...");

   }

   public Fu1(String name){

      this.name = name;

   }

   public void eat(){

      System.out.println("Fu1 eat...");

   }

}

class Zi1 extends Fu1{

   int x = 20;

   int num;

   public Zi1(String name,int num){

      //调用父类带参数的

      super(name);

      //调用本类无参构造方法

      //this();

      System.out.println("Zi1带参数的构造方法...");

   }

   public Zi1(){

      System.out.println("Zi1无参构造方法...");

   }

   public void print(){

      System.out.println("x = " +super.x);

   }

   public void eat(){

      System.out.println("大头儿子吃龙虾..");

   }

}

public class Demo8 {

   public static void main(String[] args) {

 

   }

}

方法重写

package com.cloud.day4;

/*

目前的问题:父类的功能无法满足子类的需求。


方法重写的前提:必须要存在继承的关系。

方法的重写: 子父类出了同名的函数,这个我们就称作为方法的重写。

什么是时候要使用方法的重写:父类的功能无法满足子类的需求时。


方法重写要注意的事项:

   1.方法重写时,方法名与形参列表必须一致。

   2.方法重写时,子类的权限修饰符必须要大于或者等于父类的权限修饰符。

   3.方法重写时,子类的返回值类型必须要小于或者等于父类的返回值类型。

   4.方法重写时,子类抛出的异常类型要小于或者等于父类抛出的异常类型。

         Exception(最坏)

        RuntimeException(小坏)

  

方法的重载:在一个类中存在两个或者两个以上的同名函数,称作为方法重载。

 

方法重载的要求

   1. 函数名要一致。

   2. 形参列表不一致(形参的个数或形参的类型不一致)

   3. 与返回值类型无关。

*/

 

//大的数据类型

class Animal{}

//小的数据类型

class Fish extends Animal{}

class Fu2{

   String name;

   public Fu2(String name){

      this.name = name;

   }

   public Animal eat() throws RuntimeException{

      System.out.println(name+"eat...");

      return new Animal();

   }

}

class Zi2 extends Fu2{

   String num;

   public Zi2(String name){

      super(name);//指定调用父类带参的构造方法

   }

   //重写父类的eat方法

   public Animal eat() throws RuntimeException{

      System.out.println("吃点开胃菜..");

      System.out.println("喝点汤....");

      System.out.println("吃点龙虾....");

      return new Animal();

   }

}

public class Demo9 {

   public static void main(String[] args) {

      Zi2 z = new Zi2("大头儿子");

      z.eat();

   }

}

继承案例

package com.cloud.day4;

 

/*

需求:使用java描述一下普通的学生、 java基础班的学生、就业班的学生。

所有的学生都会学习。但是学习的内容不一样。

普通的学生:马克思列宁主义。

基础班的学生:学习的是 javase。

就业班学生: javaee+android.

*/

class Student1{

   String name;

   public Student1(String name){

      this.name = name;

   }

   public void study(){

      System.out.println(name+"study..1...");

   }

}

class BaseStudent extends Student1{

   public BaseStudent(String name) {

      super(name);

   }

   public void study(){

      System.out.println(name+"study...java...");

   }

}

public class Demo10 {

   public static void main(String[] args) {

     

   }

}

Instanceof关键字

package com.cloud.day4;

/*

instanceof关键字

instanceof关键字的作用:判断一个对象是否属于指定的类别。

instanceof关键字的使用前提:判断的对象与指定的类别必须要存在继承或者实现的关系。

instanceof关键字的使用格式:

      对象  instanceof 类别

instanceof关键字的作用:到了多态之后就非常有用。

一般我们做强制类型转换之前都会使用该关键字先判断一把,然后在进行转换的。

*/

class Animal1{

   String name;

   String color;

   public Animal1(String name,String color){

      this.name = name;

      this.color = color;

   }

}

class Dog extends Animal1{

   public Dog(String name, String color) {

      super(name, color);

   }

   public void bite(){

      System.out.println(name+"bite...");

   }

}

class Mouse extends Animal1{

   public Mouse(String name, String color) {

      super(name, color);

   }

   public void dig(){

      System.out.println(name+"打洞...");

   }

}

public class Demo11 {

   public static void main(String[] args) {

      Dog d = new Dog("哈士奇","白色");

      System.out.println(d instanceof Dog);

      System.out.println(d instanceof Animal1);

      Animal1 a = new Animal1("小白","黑的");

      System.out.println(a instanceof Dog);

   }

}

Final关键字

package com.cloud.day1;

/*

final(最终、修饰符) 

final关键字的用法:

   1. final关键字修饰一个基本类型的变量时,该变量不能重新赋值,第一次的值为最终的。

   2. fianl关键字修饰一个引用类型变量时,该变量不能重新指向新的对象。

   3. final关键字修饰一个函数的时候,该函数不能被重写。

   4. final关键字修饰一个类的时候,该类不能被继承。

常量的修饰符一般为: public static final

*/

class Circle{

   double r;

   public static final double pi = 3.14;

   public Circle(double r){

      this.r = r;

   }

   public final void getArea(){

      System.out.println("面积:"+pi*r*r);

   }

}

public class Demo1 extends Circle{

   public Demo1(double r) {

      super(r);

   }

   public static void main(String[] args) {

      Circle c = new Circle(4.0);

      c.getArea();

   }

}

抽象类

package com.cloud.day1;

/*

抽象类:

目前存在的问题:

   1. 动物类的run方法描述的不正确。

   2. 没有强制要子类一定要重写run方法。

 

抽象类的应用场景:

   我们在描述一类事物的时候,发现该种事物确实存在着某种行为,

但是这种行为目前是不具体的,那么我们可以抽取这种行为的声明,但是

不去实现该种行为,这时候这种行为我们称作为抽象的行为,我们就需要使用抽象类。

 

抽象类的好处: 强制要求子类一定要实现指定的方法。

抽象类要注意的细节:

   1. 如果一个函数没有方法体,那么该函数必须要使用abstract修饰,把该函数修饰成抽象的函数。。

   2. 如果一个类出现了抽象的函数,那么该类也必须使用abstract修饰。

   3. 如果一个非抽象类继承了抽象类,那么必须要把抽象类的所有抽象方法全部实现。

   4. 抽象类可以存在非抽象方法,也可以存在抽象的方法.

   5. 抽象类可以不存在抽象方法的。

   5. 抽象类是不能创建对象的。

      疑问:为什么抽象类不能创建对象呢?

      因为抽象类是存在抽象方法的,如果能让抽象类创建对象的话,那么使用抽象的对象.调用抽象方法是没有任何意义的。

   6. 抽象类是存在构造函数的,其构造函数是提供给子类创建对象的时候初始化父类的属性的。

*/

abstract class Animal{

   String name;

   String color;

   public Animal(String name,String color){

      this.name = name;

      this.color = color;

   }

   public void eat(){

      System.out.println(name+"eat...");

   }

   public abstract void run();

}

class Dog extends Animal{

   public Dog(String name, String color) {

      super(name, color);

   }

   public void run(){

      System.out.println(name+"跑啊跑...");

   }

}

public class Demo2 {

   public static void main(String[] args) {

      Dog d = new Dog("哈哈", "白色");

      d.run();

      //Animal a = new Animal("11","22");

      //a.run();

   }

}

继承中的值传递

package com.cloud.day1;

/*

java 单继承特点,但是可以间接多继承

*/

class Ye{

   String name;

}

class Fu extends Ye{}

class Zi extends Fu{}

public class Demo5 {

   public static void main(String[] args) {

      Zi z = new Zi();

      z.name = "haha";

   }

}

间接继承

package com.cloud.day1;

/*

java 单继承特点,但是可以间接多继承

*/

class Ye{

   String name;

}

class Fu extends Ye{}

class Zi extends Fu{}

public class Demo5 {

   public static void main(String[] args) {

      Zi z = new Zi();

      z.name = "haha";

   }

}

 

继承案例

package com.cloud.day1;

/*

需求:描述一个图形、圆形、矩形三个类。不管哪种图形都会具备计算面积

与周长的行为,但是每种图形计算的方式不一致而已。

常量的命名规范:全部字母大写,单词与单词之间使用下划线分隔。

abstract不能与以下关键字共同修饰一个方法:

   1. abstract不能与private共同修饰一个方法。

   2. abstract 不能与static共同修饰一个方法。

   3. abstract 不能与final共同修饰一个方法。

*/

abstract class MyShap{

   String name;

   public MyShap(String name){

      this.name = name;

   }

   public abstract void getArea();

   public abstract void getLength();

}

class Circle1 extends MyShap{

   double r;

   public static final double PI = 3.14;

   public Circle1(String name,double r) {

      super(name);

      this.r = r;

   }

   @Override

   public void getArea() {

      System.out.println(name+"的面积:"+PI*r*r);

   }

   @Override

   public void getLength() {

      System.out.println(name+"的周长:"+2*r*PI);

   }

}

class Rect extends MyShap{

   int width;

   int height;

   public Rect(String name,int width,int height) {

      super(name);

      this.height = height;

      this.width = width;

   }

   @Override

   public void getArea() {

      System.out.println(name+"面积:"+height*width);

   }

   @Override

   public void getLength() {

      System.out.println(name+"周长:"+2*(height+width));

   }

}

public class Demo3 {

   public static void main(String[] args) {

      Circle1 c1 = new Circle1("aa",6);

      c1.getArea();

      c1.getLength();

      Rect rt = new Rect("bb", 2, 4);

      rt.getArea();

      rt.getLength();

   }

}

 

Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company pta题目java的继承覆盖综合题源码,全部内容为自己原创,能够运行但是有些代码可能写的不是很精简,这个摘要也太长了吧,凑不够字数啊 立即下载

相关推荐

电脑版微信多开,最详细教程没有之一

首先桌面任意的位置右键新建文本文档输入如下代码: TASKKILL /F/IM WECHAT.EXE start " " "D:\Program Files (x86)\Tencent\WeChat\WeChat.exe" start " " "D:\Program Files (x86)\Tencent\WeChat\WeChat.exe" ...

大超子 4390

7-2 jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company (20 分)

7-2 jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company (20 分) 文章目录7-2 jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company (20 分)提示main方法说明输入样例:输出样例:参考答案 定义Person抽象类,Student类、Company类,...

优秀的亮亮 1万+

人脸情绪识别(使用深度学习和OpenCV)

基于opencv与深度学习的人脸情绪识别系统

mx0390304的博客 8251

Java面向对象——继承(超详细讲解)

Java继承:是面向对象最显著的一个特性。继承是从已有的类中派生出新的类,新的类能吸收已有类的数据属性和行为,并能扩展新的能力。 JAVA不支持多继承,单继承使JAVA的继承关系很简单,一个类只能有一个父类,易于管理程序,同时一个类可以实现多个接口,从而克服单继承的缺点。......

Best_Basic的博客 5952

PLSQL 14.0.6 下载使用教程

众所周知,PLSQL 只是一个oracle第三方开发测试工具,它依赖oracle 客户端搭配使用,但是oracle 客户端有2G,相当庞大,不建议安装。所以我们如果下载安装好PLSQL,再下载好轻量级的支持远程链接的oracle客户端,这样搭配使用可以的话就最好了,答案是可以的。 第一步: PLSQL 下载与安装 PLSQL 14.0.6 下载地址 : https://www.allroundautomations.com/registered-plsqldev/ 下载下来,除了安装路径更改一下,

qq976511508的博客 2134

【Go语言实战】(26) 分布式搜索引擎

基于go语言的分布式搜索引擎

面向生活编程 2379

#Java学习#习题五

文章目录判断题单选题填空题123456程序填空题12编程题jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company字符串反转图书价格汇总数组元素交换 其他JAVA学习的内容见:目录 判断题 1-1.java语言中不用区分字母的大写小写。 F 1-2.在Java程序中,可以使用private来修饰一个类。 T 1-3.接口中的方法默认是public abstract方法。 T 1-4.一个类可以实现多个接口。 T 1-5.异常也

vircorns的博客 9573

通信原理 | 窗函数 | 矩形窗 | 汉宁窗 | 汉明窗 | 布莱克曼窗 | 补零对频谱的影响

比如,原信号是一个周期的余弦信号,又给它补了9个周期长度的0,那么信号并不是10个周期的余弦信号,而是一个周期的余弦加一串0,补零并没有带来新的信息!频谱分辨率的含义是两个频率不同的信号在频率上可分,也就要求他们不能落到一个频域函数的主瓣上,所以,如果待分析的两个信号频率接近,而时域长度又短,那么在频域上他们就落在一个频域函数的主瓣内了,补再多的零也无济于事。而形状(主瓣宽度)是由补零之前的信号长度决定的,补零的作用只是细化了频域序列,并没有改变其主瓣宽度。所以,汉宁窗牺牲了分辨率,换来了旁瓣泄露的减小。

若北辰 1305

Java--面向对象->继承

继承的作用:继承可以使用现有类的全部功能,并在无需重新编写原来类的情况下对功能进行扩展 1.继承的实现 在Java中,继承用extends关键字实现,定义的语法:class 子类 extends 父类 举例:未扩重新属性 class People{ private String Name; private int age; public void ...

wl 284

【低照度图像增强系列】使用MSRCR(Multi-Scale Retinex with Color Restoration)算法对低照度图像进行增强处理

【低照度图像增强系列】使用MSRCR(Multi-Scale Retinex with Color Restoration)算法对低照度图像进行增强处理

静谧、淡雅 648

零停机更新黑科技:Spring Boot应用实现无缝升级的实战指南

本文介绍了基于Spring Boot和Tomcat实现零停机更新的技术方案,通过动态端口切换和优雅关闭机制,确保服务更新过程中业务不中断。文章详细解析了核心原理,包括内嵌Tomcat的工作机制、零停机更新的三大技术基石(端口动态切换、优雅关闭和进程接管)。同时提供了完整的Java实现代码,重点展示了端口动态检查、参数动态调整、旧进程优雅终止等关键逻辑。该方案适用于高可用系统升级场景,能够有效避免传统部署方式导致的服务中断问题,实现100%无感知的应用更新。

墨夶的博客 818

jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company

jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company PTA题目分析答案不判断null的代码(非标准)判断null的代码(标准) 题目 分析 答案 发现可以不判断输入字符串null也可以编译通过 不判断null的代码(非标准) import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Collections; import java.util.L

qq_43427314的博客 7039

点云结构面智能识别与裂隙网络分析系统

点云结构面智能识别系统摘要 本系统采用深度学习与传统几何算法融合的方法,实现岩石结构面的智能识别与分析。系统包含7个核心模块:点云预处理、结构面识别、分组分割、参数计算、粗糙度分析、裂隙网络生成和可视化。技术路线结合PointNet++深度学习架构与RANSAC、区域生长等传统算法,通过预处理→深度学习识别→几何精修→参数计算→网络构建的流程实现高精度分析。预处理模块实现点云降噪、下采样和法向量估计,深度学习模块基于改进的PointNet++架构进行特征提取和分类。系统支持多种点云格式输入,提供完整的结构面

huanghm88的专栏 345

Java面向对象之继承,深度剖析继承的使用

让我们一起领略面向对象之继承的奥妙吧!

一年,我很需要这一年 2173

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

【Java】面向对象:继承、组合和多态

面向对象三大特征之继承和多态

trong_ 的烂笔头 1553

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

Java面向对象之继承

面向对象编程的特点:1、可维护性2、可扩展性3、可移植性(可重用性)4、灵活性好。

weixin_48793295的博客 620

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

java面向对象 继承_java面向对象之继承

在java中,面向对象毫无疑问是java的重中之重,今天我们来看一看java面向对象的三大特性之一继承。首先我们来了解一下类的结构是怎样的?类中主要有成员属性,成员方法,静态属性,静态方法,构造方法(暂不考虑代码块)。成员属性和成员方法是属于对象的,静态属性和静态方法是属于类的,如下图所示。类的结构我们知道了(为后面做铺垫的),那么接下来就正式的进入了我们的主题,继承!1.什么是继承?(1)用来提...

weixin_42476061的博客 194

Java面向对象:继承

Java面向对象:继承

weixin_58618735的博客 1335

Java面向对象编程【高级篇】之继承

本文介绍了Java面向对象编程中的继承机制,包括继承的定义、语法、权限修饰符及其访问范围,以及继承的特点。继承通过extends关键字实现,允许子类复用父类非私有成员,支持单继承和多层继承。权限修饰符(private、默认、protected、public)控制成员的访问范围。Java中所有类都隐式继承Object类,且方法调用遵循就近原则。继承提高了代码复用性并建立了类层次关系。

2402_88461821的博客 1204

Java 面向对象编程--继承

Java 面向对象编程--继承

黑白土豆的博客 1214

Java面向对象与继承

访问成员变量:关注创建对象时赋值符号的左侧是子类还是父类。如果是父类,则访问的是父类成员变量,如果是子类,则访问的是父类非私有成员和子类自身的成员变量访问成员方法:关注创建对象时赋值符号的右侧是调用子类构造函数和父类构造函数。如果是父类,则访问的是父类中的成员方法,如果是子类,则访问的是父类中非私有的成员方法、子类重写的父类的方法和子类自身的成员方法super。

小何代码咖啡馆 1195

【Java】面向对象之——继承

1.继承的基本概念在讲解继承的基本概念之前,读者 可以先想一想这样一个问题:现在假设 有 一 个 Person类 ,里面有name与age 两个属性,而另外一个Student类,需要有name3 个属性,具体代码如下。Person类从上面的代码中可以发现Person类中己经存在n am e和a g e两个属性,所以不希望在Student类中再重新声明这两个属性,这个时候就需要考虑是不是可以将Person类中的内容继续保留到Student类中,这也就是继承所要解决的问题。

m0_74956284的博客 1183

JAVA面向对象:继承

java面向对象:继承 继承: 关键字 extends 子类可以调用父类中的属性和方法(public 或 protected修饰的,或没有加修饰符的) 当做自己的来使用。例:Cat 类继承了 Animal 类。Animal 中有私有的 foot 属性和私有的 test4 方法。Cat 类继承了 Animal类。从图片上看到了,子类不能直接访问父类的私有成员。 子类可以添加独有的属性和方法 ...

行来行去踢桃 254

【Java】面向对象——继承

面向对象之继承

weixin_46437504的博客 813

java面向对象之继承

面向对象三大特征之一,可以把多个子类中重复的代码抽取到父类中,子类可以使用,减少代码冗余,提高代码的复用性。

xpy2428507302的博客 1023
上一篇: Spring中bean装配问题
下一篇: Servlet基本概念
午-夜
博客等级 码龄12年 95粉丝 133原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值