漫漫Java学习路,第九天

编程入门 行业动态 更新时间:2024-10-27 01:23:10

漫漫Java学习路,<a href=https://www.elefans.com/category/jswz/34/1744057.html style=第九天"/>

漫漫Java学习路,第九天

package cn.tedu.oop;
import java.util.InputMismatchException;
import java.util.Scanner;
//本类用作异常的入门案例
public class ExceptionDemo {public static void main(String[] args) {//method1();//调用暴露异常的方法//method2();//调用解决异常的方案1--捕获处理--自己解决//method3();//调用解决异常的方案2--向上抛出--别人解决f();}private  static void f(){try {method3();}catch (Exception e){System.out.println("输入的数据不对");}}/*如果一个方法抛出了异常,那么谁来调用这个方法,谁就需要处理这个异常* 这里的处理也有两种方法:捕获解决 或者 继续向上抛出* 但注意:我们一般会在main()调用之前将异常解决掉* 而不是将问题抛给main(),因为没人解决了,该报错还是报错*//*异常抛出的格式:在方法的小括号和大括号之间,写:throws 异常类型* 如果有多个异常,使用逗号分隔即可*//*private static void method3()* throws ArithmeticException,InputMismatchException,Exception{*/private static void method3() throws Exception{System.out.println("请您输入要计算的第一个整数");int a = new Scanner(System.in).nextInt();System.out.println("请您输入要计算的第二个整数");int b = new Scanner(System.in).nextInt();System.out.println(a/b);}/*异常捕获处理的格式:* try{*       可能会抛出异常的代码* }catch(预先设想的异常类型 异常名){*       万一捕获到了异常,进行处理的解决方案* }* try-catch结构可以嵌套,如果有多种异常类型需要特殊处理的话*/private static void method2() {try{System.out.println("请您输入要计算的第一个整数");int a = new Scanner(System.in).nextInt();System.out.println("请您输入要计算的第二个整数");int b = new Scanner(System.in).nextInt();System.out.println(a/b);}catch (ArithmeticException e){System.out.println("除数不能为0!");}catch (InputMismatchException e){System.out.println("请输入规定的整数!");/*使用多态的思想,不论是什么子异常,统一看做父类型Exception* 做出更加通用的解决方案,甚至可以只写这一个,上面两个不写了*/}catch(Exception e){System.out.println("您输入的数据不对,请重新输入");}}private static void method1() {System.out.println("请您输入要计算的第一个整数");int a = new Scanner(System.in).nextInt();System.out.println("请您输入要计算的第二个整数");int b = new Scanner(System.in).nextInt();System.out.println(a/b);//输入10和0,报错:ArithmeticException--算数异常,除数不能为0,数学规定//输入10和3.4,报错:InputMismatchException--输入不匹配异常/*1.不要害怕BUG,真正的勇士敢于直面自己写的BUG*//*2.学会看报错的信息提示,确定自己错误的方向*//*3.学会看报错的行号提示,确定自己报错的位置,哪里不对点哪里* 注意:源码不会错,要看自己写的代码*/}
}
package cn.tedu.oop;
import java.util.Scanner;
//本类用作异常的入门案例
public class ExceptionDemo2 {public static void main(String[] args) {aaa();}private static void aaa() {try {method();}catch(Exception e){System.out.println("数据异常");}}private static void method() throws Exception{System.out.println("请您输入要计算的第一个整数");int a = new Scanner(System.in).nextInt();System.out.println("请您输入要计算的第二个整数");int b = new Scanner(System.in).nextInt();System.out.println(a/b);}
}
package cn.tedu.oop;
//本类用作多态回顾
public class TestDemo {public static void main(String[] args) {Animal a = new Animal();Cat c = new Cat();Dog d = new Dog();a.eat();c.eat();d.eat();/*父类不能调用子类的特有功能*///a.jump();//a.run();c.jump();d.run();/*口诀1:父类引用指向子类对象* 口诀2:编译看左边,运行看右边*/Animal a1 = new Cat();Animal a2 = new Dog();a1.eat();a2.eat();//a1.jump();//多态对象把自己看做是父类类型,父类没有定义这个功能//a2.run();}
}
class Animal{public void eat(){System.out.println("咋都好吃~");}
}
class Cat extends Animal{public void eat(){System.out.println("小猫爱吃小鱼干~");}public void jump(){System.out.println("小猫出门打猎了");}
}
class Dog extends Animal{public void eat(){System.out.println("小狗爱吃肉骨头~");}public void run(){System.out.println("小狗下地种田了");}
}
package cn.tedu.oop;
//本类用于测试多态成员的使用情况
public class TestDemo2 {public static void main(String[] args) {Dog2 d = new Dog2();System.out.println(d.sum);d.eat();Animal2 a = new Dog2();/*多态中,成员变量使用的是父类的*/System.out.println(a.sum);/*多态中,方法的声明使用的是父类的,方法体使用的是子类的*/a.eat();/*多态中调用的静态方法是父类的,因为多态对象把自己看做是父类类型* 直接使用父类中的静态资源*/a.play();Animal2.play();Dog2.play();}
}
class Animal2{int sum = 10;public void eat(){System.out.println("王八看绿豆~");}public static void play(){System.out.println("玩蛋去吧!!!");}
}
class Dog2 extends Animal2{int sum = 20;public void eat(){System.out.println("御史吃屎~");}//@Override/*这不是一个重写的方法,只是恰巧在两个类中出现了一模一样的两个静态方法* 注意:静态方法也属于静态资源,静态资源是属于类的,只有一份* 在哪个类里定义,就作为哪个类的资源来使用,不存在重写的现象*/public static void play(){System.out.println("拿你的脑壳给我磨磨牙!!!");}
}
package cn.tedu.oop2;
//本类用作抽象类构造函数测试
/*抽象类是否有构造方法?-->有
* 既然抽象类不能实例化,为什么要有构造方法?
* 不是为了自己使用,而是为了子类创建对象时使用super();*/
class AbstractDemo2 {public static void main(String[] args) {//Animal2 a = new Animal2();Pig2  p = new Pig2();}
}
abstract class Animal2{public Animal2(){System.out.println("小兔子白又白,两只耳朵拎起来");}
}
class Pig2 extends Animal2{public Pig2(){System.out.println("红烧很好吃");}
}
package cn.tedu.oop2;
//本类用作测试抽象的入门案例
public class AbstractDemo {public static void main(String[] args) {/*4.抽象类不可以实例化!!!--创建对象*///Animal a = new Animal();Animal a = new Pig();a.eat();a.play();a.fly();a.fly2();}
}
/*2.被abstract修饰的类是抽象类
* 如果一个类中包含了抽象方法,那么这个类必须被声明成一个抽象类*/
abstract class Animal{public void eat(){System.out.println("中华料理博大精深");}public void play(){System.out.println("食物可不是用来玩的");}/*1.被abstract修饰的方法是抽象方法,抽象方法没有方法体*/public abstract void fly();public abstract void fly2();
}
/*3.当一个子类继承了抽象父类后,有两种解决方案:
* 方案一:变成抽象子类,"躺平,我也不实现,继续抽象"
* 方案二:变成普通子类,"父债子偿,子类需要实现抽象父类中所有的抽象方法"*/
class Pig extends Animal{public void fly() {System.out.println("站在风口上,猪都能上天");}public void fly2(){System.out.println("抽象父类中的所有抽象方法都需要被实现");}
}
package cn.tedu.oop2;
//本类用作抽象类中的成员测试
public class AbstractDemo3 {}
abstract class Fruit{/*1.抽象类中可以定义成员变量吗?-->可以*/String name = "苹果";/*2.抽象类中可以定义成员常量吗?-->可以*/final String NAME = "小黄人";/*3.抽象类中可以定义普通方法吗?-->可以*/public void sum(){System.out.println("往树下尿尿");}/*4.抽象类中可以定义抽象方法吗?-->可以*/public abstract void eat();/*5.抽象类中可以全是普通方法吗?-->可以*/public void grow(){System.out.println("多晒太阳");}/*如果一个类中都是普通方法,那他为什么还要被修饰成抽象类呢?* 因为:抽象类不可以被实例化,所以如果不想让外界创建本类的对象* 就可以把普通类声明成抽象类*//*6.抽象类中可以全是抽象方法吗?-->可以*/public abstract void play();
}
class Banana extends Fruit{public void eat() {}public void play() {}
}
package cn.tedu.oop2;
//练习作业
public class DemoTest {public static void main(String[] args) {Brid b1 = new Pigeon();System.out.println(b1.legNumbers);System.out.println(b1.eggNumbers);//无法以父类对象调用子类成员变量b1.fly();b1.layEggs();Swallow s = new Swallow();s.fly();s.layEggs();s.makeNest();System.out.println(s.legNumbers);System.out.println(s.eggNumbers);}
}
/*1.定义父类小鸟类Bird:
* 腿legNumbers的数量为2
* 下蛋eggNumbers数量可以自定义
* 拥有飞行fly()功能 下蛋layEggs()功能*/
abstract class Brid{final int legNumbers = 2;int eggNumbers;public void fly(){System.out.println("傻了吧!爷会飞!!!");}abstract public void layEggs();
}
//2.定义子级鸽子类Pigeon:它既会飞,也会下蛋
class Pigeon extends Brid{int eggNumbers = 2;public void layEggs(){System.out.println("又来了"+eggNumbers+"次大姨妈啦~");}
}
//3.定义子级燕子类Swallow:它既会飞,也会下蛋,还有自己的特有功能筑巢makeNest()
class Swallow extends Brid{int eggNumbers = 6;public void layEggs(){System.out.println("大姨妈你来的也太勤快啦~已经是这周第"+eggNumbers+"次了");}public void makeNest(){System.out.println("吃燕窝吗?我亲口吐的!");}
}
package cn.tedu.oop2;
//练习作业
public class DemoTest2 {public static void main(String[] args) {Ant a = new Ant();System.out.println(a.legNumbers);System.out.println(a.eggNumbers);a.fly();a.spawn();Bee b = new Bee();System.out.println(b.legNumbers);System.out.println(b.eggNumbers);b.fly();b.spawn();b.makeHoney();}
}
//1.定义父类昆虫类Insect:
// 腿的数量为6,产卵的数量可以自定义,拥有飞行的功能还有产卵spawn的功能
abstract class Insect{final int legNumbers = 6;int eggNumbers;public void fly(){System.out.println("傻了吧!爷也会飞~");}abstract public void spawn();
}
//2.定义子级蚂蚁类Ant:它既会飞,也会产卵
class Ant extends Insect{int eggNumbers = 1000;public void spawn() {System.out.println("我贼能生!一次生"+eggNumbers+"个娃");}
}
//3.定义子级蜜蜂类Bee:它既会飞,也会产卵,还有自己的特有功能制作蜂蜜makeHoney
class Bee extends Insect{int eggNumbers = 500;public void spawn() {System.out.println("虽然我没上面那位兄弟能生,但我一次也能生"+eggNumbers+"个娃");}public void makeHoney(){System.out.println("蜂蜜甜吗?我拉的~");}
}
package cn.tedu.oop2;
//本类用于面向抽象编程的推导
public class DesignTeacher {
}
abstract class Teacher{String name;int id;public abstract void ready();public abstract void teach();
}
class CGBTeacher extends Teacher{public void ready(){System.out.println("正在备课:互联网架构与微服务");}public void teach(){System.out.println("正在讲课:互联网架构与微服务");}
}
class ACTTeacher extends Teacher{public void ready(){System.out.println("正在备课:基础加强 框架加强 高新技术");}public void teach(){System.out.println("正在讲课:基础加强 框架加强 高新技术");}
}
abstract class SCDTeacher extends Teacher{public void ready() {System.out.println("正在研发中....");}
}
package cn.tedu.oop2;
//本类用于练习多态的使用
public class TestCar {public static void main(String[] args) {Car c = new Car();System.out.println(c.getColor());System.out.println(c.getBrand());System.out.println(c.getId());System.out.println(c.getPrice());c.start();c.stop();BMW b = new BMW();System.out.println(b.getColor());System.out.println(b.color);b.start();b.stop();Car c1 = new TSL();//System.out.println(c1.color);System.out.println(c1.getColor());c1.start();c1.stop();//c1.swim();}
}
class Car{private String brand;private String color;private int id;private double price;public void start(){System.out.println("今晚八点,秋名山下,败者留下车标");}public void stop(){System.out.println("排水渠过弯");}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public int getId() {return id;}public void setId(int id) {this.id = id;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}
}class BMW extends Car{String color = "公爵紫";public void start(){System.out.println("快闪开,我要螺旋升天啦~");}
}
class TSL extends Car{public void stop(){System.out.println("刹车只是装饰,飞行员不需要刹车!");}public void swim(){System.out.println("傻了吧,爷会潜水~");}
}

更多推荐

漫漫Java学习路,第九天

本文发布于:2024-02-17 10:09:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1693648.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第九天   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!