第1关:类的继承和super关键字
java
package step1;
class Animal{
String name;
public void shout(){
System.out.println(this.name + "发出叫声");
}
public Animal(String name) {
this.name = name;
}
}
// ---------------------Begin------------------------
//编写Dog类继承Animal类,重写shout方法,增加superShout方法
class Dog extends Animal{
public Dog(String name) {
super(name);
}
public void shout(){
System.out.println(this.name+"发出汪汪叫");
}
public void superShout(){
super.shout();
}
}
// ---------------------End------------------------
第2关:final关键字改错
java
package step2;
// ---------------------Begin------------------------
class Person{
int age;
String name;
public void sayHello(){
System.out.println("nice to meet you.");
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Student extends Person{
@Override
public void sayHello() {
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));
}
}
// ---------------------End------------------------
public class FinalKeyWord {
public static void main(String[] args) {
Student student = new Student();
student.setAge(18);
student.setName("张三");
student.sayHello();
}
}
第3关:抽象类
java
package step3;
// ---------------------Begin------------------------
abstract class Studenta {
abstract void sayHello();
}
class Student extends Person {
String name="jack";
int age=18;
public Student(int age,String name){
super(age,name);
}
public void sayHello() {
System.out.println(String.format("大家好,我叫%s,今年%d岁,很高兴认识大家", this.name,this.age));
}
public static void main(String[] args) {
Student s=new Student();
s.sayHello();
}
}
// ---------------------End------------------------
第4关:接口
java
package step4;
// ---------------------Begin------------------------
interface MyInterface{
void sayHi();
}
class InterfaceDemo implements Demo{
public void show(){
System.out.println("实现了接口中的show方法");
}
}
// ---------------------End------------------------
第5关:多态
java
package step5;
// ---------------------Begin------------------------
class Cat extends Animal{
public void shout(){
System.out.println("小花猫喵喵叫");
}
}
class Dog extends Animal{
public void shout(){
System.out.println("大黄狗汪汪叫");
}
}
class Pig extends Animal{
public void shout(){
System.out.println("大胖猪喊喊叫");
}
}
// ---------------------End------------------------
class polymorphicDemo{
public static void main(String[] args){
Cat c = new Cat();
Dog d = new Dog();
method(c);
method(d);
method(new Pig());
}
public static void method(Animal a){
a.shout();
}
}
第6关:匿名内部类
java
package step6;
abstract class Demo{
abstract void show();
}
// ---------------------Begin------------------------
class Outer{
public void show(){
System.out.println("外部类调用show方法");
}
void method(){
new Demo(){
void show(){
System.out.println("内部类调用show方法");
}
}.show();
}
}
// ---------------------End------------------------
第7关:Lambda表达式
java
package step7;
import java.util.Scanner;
interface Mycaculator{
int add(int a, int b);
}
public class LambdaTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
// ---------------------Begin------------------------
//lambad表达式实现Mycaculator接口完成两数相加并打印输出结果
Mycaculator calculator = (x,y)->x+y;
int result = calculator.add(a,b);
System.out.print(result);
// ---------------------End------------------------
}
}
第8关:异常处理
java
package step8;
public class ErrorHandle {
public static void main(String[] args) {
ErrorHandle errorHandle = new ErrorHandle();
// ---------------------Begin------------------------
//捕获errorSrc对象中divideTest方法中的除零异常(ArithmeticException),获取异常信息可以使用getMessage方法
try{
errorHandle.divideTest();
}
catch (ArithmeticException e){
System.out.println("Exception Messaage:"+e.getMessage());
}
finally {
System.out.println("/ by zero");
System.out.println("成功捕获除零异常");
}
}
public void divideTest() {
try {
int result = 0 / 1;
}
catch (ArithmeticException e) {
System.out.println("Exception Message: " + e.getMessage());
throw e;
}
}
}
第9关:知识回顾
1c 2c 3c 4b 5bc