当前位置:   article > 正文

头歌Java实验面向对象高级_头歌java面向对象高级第8关:异常处理

头歌java面向对象高级第8关:异常处理

第1关:类的继承和super关键字

  1. package step1;
  2. class Animal{
  3. String name;
  4. public void shout(){
  5. System.out.println(this.name + "发出叫声");
  6. }
  7. public Animal(String name) {
  8. this.name = name;
  9. }
  10. }
  11. // ---------------------Begin------------------------
  12. //编写Dog类继承Animal类,重写shout方法,增加superShout方法
  13. class Dog extends Animal{
  14. public Dog(String name) {
  15. super(name);
  16. }
  17. public void shout(){
  18. System.out.println(this.name+"发出汪汪叫");
  19. }
  20. public void superShout(){
  21. super.shout();
  22. }
  23. }
  24. // ---------------------End------------------------

第2关:final关键字改错

  1. package step2;
  2. // ---------------------Begin------------------------
  3. class Person{
  4. int age;
  5. String name;
  6. public void sayHello(){
  7. System.out.println("nice to meet you.");
  8. }
  9. public int getAge() {
  10. return age;
  11. }
  12. public void setAge(int age) {
  13. this.age = age;
  14. }
  15. public String getName() {
  16. return name;
  17. }
  18. public void setName(String name) {
  19. this.name = name;
  20. }
  21. }
  22. class Student extends Person{
  23. @Override
  24. public void sayHello() {
  25. System.out.println(String.format("Hey nice to meet you. I am a student, my name is %s. I am %d years old.", this.name, this.age));
  26. }
  27. }
  28. // ---------------------End------------------------
  29. public class FinalKeyWord {
  30. public static void main(String[] args) {
  31. Student student = new Student();
  32. student.setAge(18);
  33. student.setName("张三");
  34. student.sayHello();
  35. }
  36. }

第3关:抽象类

  1. package step3;
  2. // ---------------------Begin------------------------
  3. abstract class Studenta {
  4. abstract void sayHello();
  5. }
  6. class Student extends Person {
  7. String name="jack";
  8. int age=18;
  9. public Student(int age,String name){
  10. super(age,name);
  11. }
  12. public void sayHello() {
  13. System.out.println(String.format("大家好,我叫%s,今年%d岁,很高兴认识大家", this.name,this.age));
  14. }
  15. public static void main(String[] args) {
  16. Student s=new Student();
  17. s.sayHello();
  18. }
  19. }
  20. // ---------------------End------------------------

第4关:接口

  1. package step4;
  2. // ---------------------Begin------------------------
  3. interface MyInterface{
  4. void sayHi();
  5. }
  6. class InterfaceDemo implements Demo{
  7. public void show(){
  8. System.out.println("实现了接口中的show方法");
  9. }
  10. }
  11. // ---------------------End------------------------

第5关:多态

  1. package step5;
  2. // ---------------------Begin------------------------
  3. class Cat extends Animal{
  4. public void shout(){
  5. System.out.println("小花猫喵喵叫");
  6. }
  7. }
  8. class Dog extends Animal{
  9. public void shout(){
  10. System.out.println("大黄狗汪汪叫");
  11. }
  12. }
  13. class Pig extends Animal{
  14. public void shout(){
  15. System.out.println("大胖猪喊喊叫");
  16. }
  17. }
  18. // ---------------------End------------------------
  19. class polymorphicDemo{
  20. public static void main(String[] args){
  21. Cat c = new Cat();
  22. Dog d = new Dog();
  23. method(c);
  24. method(d);
  25. method(new Pig());
  26. }
  27. public static void method(Animal a){
  28. a.shout();
  29. }
  30. }

第6关:匿名内部类

  1. package step6;
  2. abstract class Demo{
  3. abstract void show();
  4. }
  5. // ---------------------Begin------------------------
  6. class Outer{
  7. public void show(){
  8. System.out.println("外部类调用show方法");
  9. }
  10. void method(){
  11. new Demo(){
  12. void show(){
  13. System.out.println("内部类调用show方法");
  14. }
  15. }.show();
  16. }
  17. }
  18. // ---------------------End------------------------

第7关:Lambda表达式

  1. package step7;
  2. import java.util.Scanner;
  3. interface Mycaculator{
  4. int add(int a, int b);
  5. }
  6. public class LambdaTest {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9. int a = scanner.nextInt();
  10. int b = scanner.nextInt();
  11. // ---------------------Begin------------------------
  12. //lambad表达式实现Mycaculator接口完成两数相加并打印输出结果
  13. Mycaculator calculator = (x,y)->x+y;
  14. int result = calculator.add(a,b);
  15. System.out.print(result);
  16. // ---------------------End------------------------
  17. }
  18. }

第8关:异常处理

  1. package step8;
  2. public class ErrorHandle {
  3. public static void main(String[] args) {
  4. ErrorHandle errorHandle = new ErrorHandle();
  5. // ---------------------Begin------------------------
  6. //捕获errorSrc对象中divideTest方法中的除零异常(ArithmeticException),获取异常信息可以使用getMessage方法
  7. try{
  8. errorHandle.divideTest();
  9. }
  10. catch (ArithmeticException e){
  11. System.out.println("Exception Messaage:"+e.getMessage());
  12. }
  13. finally {
  14. System.out.println("/ by zero");
  15. System.out.println("成功捕获除零异常");
  16. }
  17. }
  18. public void divideTest() {
  19. try {
  20. int result = 0 / 1;
  21. }
  22. catch (ArithmeticException e) {
  23. System.out.println("Exception Message: " + e.getMessage());
  24. throw e;
  25. }
  26. }
  27. }

第9关:知识回顾

1c 2c 3c 4b 5bc

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/511875
推荐阅读
相关标签
  

闽ICP备14008679号