|
|
|
|
|
Java面向对象 - 封装、继承和多态的综合练习
|
|
|
泰迪male brown波斯猫male2.5预期输出:名称:泰迪,性别:male,颜色:brown,汪汪叫
|
|
|
泰迪吃骨头!名称:波斯猫,性别:male,体重:2.5kg,喵喵叫 波斯猫吃鱼!
|
|
|
package case1;
|
|
|
import java.util.Scanner;
|
|
|
public class Task1 {
|
|
|
public static void main(String[] args) {
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
String dogName = sc.next();
|
|
|
String dogSex = sc.next();
|
|
|
String dogColor = sc.next();
|
|
|
String catName = sc.next();
|
|
|
String catSex = sc.next();
|
|
|
double catWeight = sc.nextDouble();
|
|
|
Dog chs=new Dog();
|
|
|
chs.setDog(dogName,dogSex,dogColor);
|
|
|
chs.talk();
|
|
|
chs.eat();
|
|
|
Cat jq=new Cat();
|
|
|
jq.setCat(catName,catSex,catWeight);
|
|
|
jq.talk();
|
|
|
jq.eat();} }
|
|
|
|
|
|
abstract class Pet {
|
|
|
String name;
|
|
|
String sex;
|
|
|
public void setPet(String name,String sex){
|
|
|
this.name=name;
|
|
|
this.sex=sex;
|
|
|
}
|
|
|
abstract void talk();
|
|
|
abstract void eat();
|
|
|
}
|
|
|
class Dog extends Pet {
|
|
|
String color;
|
|
|
public void setDog(String name,String sex,String color){
|
|
|
this.color=color;
|
|
|
super.setPet(name,sex); }
|
|
|
public void talk(){
|
|
|
System.out.println("名称:"+this.name+",性别:"+this.sex+",颜色:"+this.color+",汪汪叫"); }
|
|
|
public void eat(){
|
|
|
System.out.println(this.name+"吃骨头!"); }}
|
|
|
class Cat extends Pet {
|
|
|
double weight;
|
|
|
public void setCat(String name,String sex,double weight){
|
|
|
this.weight=weight;
|
|
|
super.setPet(name,sex); }
|
|
|
|
|
|
public void talk(){
|
|
|
System.out.println("名称:"+this.name+",性别:"+this.sex+",体重:"+this.weight+"kg,喵喵叫"); }
|
|
|
public void eat(){
|
|
|
System.out.println(this.name+"吃鱼!"); }}
|
|
|
|
|
|
张三 男 20 史蒂文 男 22
|
|
|
预期输出:
|
|
|
姓名:张三,性别:男,年龄:20,我是中国人,我喜欢吃饭!
|
|
|
姓名:史蒂文,性别:男,年龄:22,我是英国人,我喜欢吃三明治!
|
|
|
张三在练习太极拳!
|
|
|
史蒂文在练习骑马!
|
|
|
package case2;
|
|
|
import java.util.Scanner;
|
|
|
public class Task2 {
|
|
|
public static void main(String[] args) {
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
String cName = sc.next();
|
|
|
String cSex = sc.next();
|
|
|
int cAge = sc.nextInt();
|
|
|
String eName = sc.next();
|
|
|
String eSex = sc.next();
|
|
|
int eAge = sc.nextInt();
|
|
|
Person st=new Chinese(cName,cSex,cAge);
|
|
|
showEat(st);
|
|
|
English chs=new English(eName,eSex,eAge);
|
|
|
showEat(chs);
|
|
|
Chinese st2=(Chinese)st;
|
|
|
st2.shadowBoxing();
|
|
|
English chs2=(English)chs;
|
|
|
chs2.horseRiding(); }
|
|
|
public static void showEat(Person p){
|
|
|
p.eat(); }}
|
|
|
abstract class Person {
|
|
|
abstract void eat();
|
|
|
String name;
|
|
|
String sex;
|
|
|
int age;
|
|
|
public void setPerson(String name,String sex,int age){
|
|
|
this.name=name;
|
|
|
this.sex=sex;
|
|
|
this.age=age;
|
|
|
}}
|
|
|
class Chinese extends Person {
|
|
|
public void eat(){
|
|
|
System.out.println("姓名:"+this.name+",性别:"+this.sex+",年龄:"+this.age+",我是中国人,我喜欢吃饭!"); }
|
|
|
public Chinese(String name,String sex,int age){
|
|
|
super.setPerson(name,sex,age); }
|
|
|
public void shadowBoxing(){
|
|
|
System.out.println(this.name+"在练习太极拳!"); }}
|
|
|
class English extends Person {
|
|
|
public void eat(){
|
|
|
System.out.println("姓名:"+this.name+",性别:"+this.sex+",年龄:"+this.age+",我是英国人,我喜欢吃三明治!"); }
|
|
|
public English(String name,String sex,int age){
|
|
|
super.setPerson(name,sex,age); }
|
|
|
void horseRiding(){
|
|
|
System.out.println(this.name+"在练习骑马!"); }}
|
|
|
张继科 30 易建联 31 刘国梁 42 杜锋 37
|
|
|
预期输出:
|
|
|
张继科---30
|
|
|
人都是要睡觉的
|
|
|
乒乓球运动员吃大白菜,喝小米粥
|
|
|
乒乓球运动员学习如何发球和接球
|
|
|
乒乓球运动员说英语
|
|
|
----------------
|
|
|
易建联---31
|
|
|
人都是要睡觉的
|
|
|
篮球运动员吃牛肉,喝牛奶
|
|
|
篮球运动员学习如何运球和投篮
|
|
|
----------------
|
|
|
刘国梁---42
|
|
|
人都是要睡觉的
|
|
|
乒乓球教练吃小白菜,喝大米粥
|
|
|
乒乓球教练教如何发球和接球
|
|
|
乒乓球教练说英语
|
|
|
----------------
|
|
|
杜锋---37
|
|
|
人都是要睡觉的
|
|
|
篮球教练吃羊肉,喝羊奶
|
|
|
篮球教练教如何运球和投篮
|
|
|
----------------
|
|
|
package case3;
|
|
|
import java.util.Scanner;
|
|
|
public class Task3 {
|
|
|
public static void main(String[] args) {
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
String pppName = sc.next();
|
|
|
int pppAge = sc.nextInt();
|
|
|
String bpName = sc.next();
|
|
|
int bpAge = sc.nextInt();
|
|
|
String ppcName = sc.next();
|
|
|
int ppcAge = sc.nextInt();
|
|
|
String bcName = sc.next();
|
|
|
int bcAge = sc.nextInt();
|
|
|
PingPangPlayer ppp=new PingPangPlayer(pppName,pppAge);
|
|
|
System.out.println(pppName+"---"+pppAge);
|
|
|
ppp.sleep();
|
|
|
ppp.eat();
|
|
|
ppp.study();
|
|
|
ppp.speak();
|
|
|
BasketballPlayer bp=new BasketballPlayer(bpName,bpAge);
|
|
|
System.out.println(bpName+"---"+bpAge);
|
|
|
bp.sleep();
|
|
|
bp.eat();
|
|
|
bp.study();
|
|
|
PingPangCoach ppc=new PingPangCoach(ppcName,ppcAge);
|
|
|
System.out.println(ppcName+"---"+ppcAge);
|
|
|
ppc.sleep();
|
|
|
ppc.eat();
|
|
|
ppc.teach();
|
|
|
ppc.speak();
|
|
|
BasketballCoach bc=new BasketballCoach(bcName,bcAge);
|
|
|
System.out.println(bcName+"---"+bcAge);
|
|
|
bc.sleep();
|
|
|
bc.eat();
|
|
|
bc.teach();
|
|
|
System.out.println("----------------"); }}
|
|
|
interface SpeakEnglish {
|
|
|
abstract void speak();}
|
|
|
abstract class Person {
|
|
|
String name;
|
|
|
int age;
|
|
|
public Person(String name,int age){
|
|
|
this.name=name;
|
|
|
this.age=age; }
|
|
|
public void sleep(){
|
|
|
System.out.println("人都是要睡觉的"); }
|
|
|
abstract void eat();}
|
|
|
abstract class Player extends Person {
|
|
|
public Player(String name,int age){
|
|
|
super(name,age); }
|
|
|
abstract void study();}
|
|
|
abstract class Coach extends Person {
|
|
|
public Coach(String name,int age){
|
|
|
super(name,age); }
|
|
|
abstract void teach();}
|
|
|
class PingPangPlayer extends Player implements SpeakEnglish {
|
|
|
public PingPangPlayer(String name,int age){
|
|
|
super(name,age); }
|
|
|
public void eat(){
|
|
|
System.out.println("乒乓球运动员吃大白菜,喝小米粥"); }
|
|
|
public void study(){
|
|
|
System.out.println("乒乓球运动员学习如何发球和接球"); }
|
|
|
public void speak(){
|
|
|
System.out.println("乒乓球运动员说英语"); }}
|
|
|
class BasketballPlayer extends Player {
|
|
|
public BasketballPlayer(String name,int age){
|
|
|
super(name,age); }
|
|
|
public void eat(){
|
|
|
System.out.println("篮球运动员吃牛肉,喝牛奶"); }
|
|
|
public void study(){
|
|
|
System.out.println("篮球运动员学习如何运球和投篮"); }}
|
|
|
class PingPangCoach extends Coach implements SpeakEnglish {
|
|
|
public PingPangCoach(String name,int age){
|
|
|
super(name,age); }
|
|
|
public void eat(){
|
|
|
System.out.println("乒乓球教练吃小白菜,喝大米粥"); }
|
|
|
public void teach(){
|
|
|
System.out.println("乒乓球教练教如何发球和接球"); }
|
|
|
public void speak(){
|
|
|
System.out.println("乒乓球教练说英语"); }}
|
|
|
class BasketballCoach extends Coach {
|
|
|
public BasketballCoach(String name,int age){
|
|
|
super(name,age); }
|
|
|
public void eat(){
|
|
|
System.out.println("篮球教练吃羊肉,喝羊奶"); }
|
|
|
public void teach(){
|
|
|
System.out.println("篮球教练教如何运球和投篮"); }}
|
|
|
package case3;
|
|
|
import java.util.Scanner;
|
|
|
public class Task3 {
|
|
|
public static void main(String[] args) {
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
String pppName = sc.next();
|
|
|
int pppAge = sc.nextInt();
|
|
|
String bpName = sc.next();
|
|
|
int bpAge = sc.nextInt();
|
|
|
String ppcName = sc.next();
|
|
|
int ppcAge = sc.nextInt();
|
|
|
String bcName = sc.next();
|
|
|
int bcAge = sc.nextInt();
|
|
|
PingPangPlayer ppp=new PingPangPlayer(pppName,pppAge);
|
|
|
System.out.println(pppName+"---"+pppAge);
|
|
|
ppp.sleep();
|
|
|
ppp.eat();
|
|
|
ppp.study();
|
|
|
ppp.speak();
|
|
|
BasketballPlayer bp=new BasketballPlayer(bpName,bpAge);
|
|
|
System.out.println(bpName+"---"+bpAge);
|
|
|
bp.sleep();
|
|
|
bp.eat();
|
|
|
bp.study();
|
|
|
PingPangCoach ppc=new PingPangCoach(ppcName,ppcAge);
|
|
|
System.out.println(ppcName+"---"+ppcAge);
|
|
|
ppc.sleep();
|
|
|
ppc.eat();
|
|
|
ppc.teach();
|
|
|
ppc.speak();
|
|
|
BasketballCoach bc=new BasketballCoach(bcName,bcAge);
|
|
|
System.out.println(bcName+"---"+bcAge);
|
|
|
bc.sleep();
|
|
|
bc.eat();
|
|
|
bc.teach();
|
|
|
System.out.println("----------------"); }}
|
|
|
interface SpeakEnglish {
|
|
|
abstract void speak();}
|
|
|
abstract class Person {
|
|
|
String name;
|
|
|
int age;
|
|
|
public Person(String name,int age){
|
|
|
this.name=name;
|
|
|
this.age=age; }
|
|
|
public void sleep(){
|
|
|
System.out.println("人都是要睡觉的")} }
|
|
|
abstract void eat();}
|
|
|
abstract class Player extends Person {
|
|
|
public Player(String name,int age){
|
|
|
super(name,age); }
|
|
|
abstract void study();}
|
|
|
abstract class Coach extends Person {
|
|
|
public Coach(String name,int age){
|
|
|
super(name,age); }
|
|
|
abstract void teach();}
|
|
|
class PingPangPlayer extends Player implements SpeakEnglish {
|
|
|
public PingPangPlayer(String name,int age){
|
|
|
super(name,age); }
|
|
|
public void eat(){
|
|
|
System.out.println("乒乓球运动员吃大白菜,喝小米粥"); }
|
|
|
public void study(){
|
|
|
System.out.println("乒乓球运动员学习如何发球和接球"); }
|
|
|
public void speak(){
|
|
|
System.out.println("乒乓球运动员说英语"); }}
|
|
|
class BasketballPlayer extends Player {
|
|
|
public BasketballPlayer(String name,int age){
|
|
|
super(name,age); }
|
|
|
public void eat(){
|
|
|
System.out.println("篮球运动员吃牛肉,喝牛奶"); }
|
|
|
public void study(){
|
|
|
System.out.println("篮球运动员学习如何运球和投篮"); }}
|
|
|
class PingPangCoach extends Coach implements SpeakEnglish {
|
|
|
public PingPangCoach(String name,int age){
|
|
|
super(name,age); }
|
|
|
public void eat(){
|
|
|
System.out.println("乒乓球教练吃小白菜,喝大米粥"); }
|
|
|
public void teach(){
|
|
|
System.out.println("乒乓球教练教如何发球和接球"); }
|
|
|
|
|
|
public void speak(){
|
|
|
System.out.println("乒乓球教练说英语"); }
|
|
|
/********* end *********/
|
|
|
}
|
|
|
|
|
|
class BasketballCoach extends Coach {
|
|
|
/********* begin *********/
|
|
|
public BasketballCoach(String name,int age){
|
|
|
super(name,age);
|
|
|
}
|
|
|
|
|
|
public void eat(){
|
|
|
System.out.println("篮球教练吃羊肉,喝羊奶");
|
|
|
}
|
|
|
|
|
|
public void teach(){
|
|
|
System.out.println("篮球教练教如何运球和投篮");
|
|
|
}
|
|
|
/********* end *********/
|
|
|
}
|
|
|
|
|
|
Java面向对象 - 封装、继承和多态
|
|
|
我是:张三,今年:18岁
|
|
|
package case1;
|
|
|
|
|
|
public class TestPersonDemo {
|
|
|
public static void main(String[] args) {
|
|
|
/********* begin *********/
|
|
|
// 声明并实例化一Person对象p
|
|
|
Person p =new Person();
|
|
|
p.setName("张三") ;
|
|
|
p.setAge(18) ;
|
|
|
p.talk();
|
|
|
}
|
|
|
// 给p中的属性赋值
|
|
|
|
|
|
// 调用Person类中的talk()方法
|
|
|
/********* end *********/
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
// 在这里定义Person类
|
|
|
class Person {
|
|
|
/********* begin *********/
|
|
|
private String name;
|
|
|
private int age;
|
|
|
/********* end *********/
|
|
|
public String getName() {
|
|
|
return name;
|
|
|
}
|
|
|
public void setName(String name) {
|
|
|
this.name = name;
|
|
|
}
|
|
|
public int getAge() {
|
|
|
return age;
|
|
|
}
|
|
|
public void setAge(int age) {
|
|
|
this.age = age;
|
|
|
}
|
|
|
public void talk(){
|
|
|
System.out.println("我是:"+name+",今年:"+age+"岁");
|
|
|
}
|
|
|
|
|
|
}
|
|
|
测试输入:无
|
|
|
预期输出:
|
|
|
大花猫喵喵叫
|
|
|
大花猫吃鱼
|
|
|
大花猫6岁
|
|
|
大黑狗汪汪叫
|
|
|
大黑狗吃骨头
|
|
|
大黑狗8岁
|
|
|
package case2;
|
|
|
|
|
|
public class extendsTest {
|
|
|
public static void main(String args[]) {
|
|
|
|
|
|
|
|
|
// 实例化一个Cat对象,设置属性name和age,调用voice()和eat()方法,再打印出名字和年龄信息
|
|
|
/********* begin *********/
|
|
|
Cat a =new Cat("大花猫",6);
|
|
|
a.voice ("大花猫");
|
|
|
a.eat ("大花猫");
|
|
|
System.out.println(a.getName()+a.getAge()+"岁");
|
|
|
|
|
|
|
|
|
|
|
|
/********* end *********/
|
|
|
|
|
|
// 实例化一个Dog对象,设置属性name和age,调用voice()和eat()方法,再打印出名字和年龄信息
|
|
|
/********* begin *********/
|
|
|
Dog b = new Dog("大黑狗",8);
|
|
|
b.voice ("大黑狗");
|
|
|
b.eat ("大黑狗");
|
|
|
System.out.println(b.getName()+b.getAge()+"岁");
|
|
|
/********* end *********/
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class Animal {
|
|
|
/********* begin *********/
|
|
|
private String name;
|
|
|
private int age;
|
|
|
Animal(String name,int age){
|
|
|
this.name = name;
|
|
|
this.age = age;
|
|
|
}
|
|
|
/********* end *********/
|
|
|
public String getName() {
|
|
|
return name;
|
|
|
}
|
|
|
public void setName(String name) {
|
|
|
this.name = name;
|
|
|
}
|
|
|
public int getAge() {
|
|
|
return age;
|
|
|
}
|
|
|
public void setAge(int age) {
|
|
|
this.age = age;
|
|
|
}
|
|
|
|
|
|
/********* end *********/
|
|
|
|
|
|
}
|
|
|
|
|
|
class Cat extends Animal {
|
|
|
// 定义Cat类的voice()和eat()方法
|
|
|
/********* begin *********/
|
|
|
Cat(String name,int age){
|
|
|
super(name,age);
|
|
|
}
|
|
|
void voice(String name){
|
|
|
|
|
|
|
|
|
System.out.println(name+"喵喵叫");
|
|
|
}
|
|
|
void eat(String name){
|
|
|
System.out.println(name+"吃鱼");
|
|
|
} /********* end *********/
|
|
|
}
|
|
|
|
|
|
class Dog extends Animal {
|
|
|
// 定义Dog类的voice()和eat()方法
|
|
|
/********* begin *********/
|
|
|
Dog(String name,int age){
|
|
|
super(name,age);}
|
|
|
void voice(String name){
|
|
|
System.out.println(name+"汪汪叫");
|
|
|
}
|
|
|
void eat(String name){
|
|
|
System.out.println(name+"吃骨头");
|
|
|
}
|
|
|
/********* end *********/
|
|
|
}
|
|
|
测试输入:无
|
|
|
预期输出:
|
|
|
姓名:张三,年龄:18,学校:哈佛大学
|
|
|
package case3;
|
|
|
|
|
|
public class superTest {
|
|
|
public static void main(String[] args) {
|
|
|
// 实例化一个Student类的对象s,为Student对象s中的school赋值,打印输出信息
|
|
|
/********* begin *********/
|
|
|
Student s=new Student();
|
|
|
s.name="张三";
|
|
|
s.age=18;
|
|
|
s.school="哈佛大学";
|
|
|
|
|
|
s.b();
|
|
|
/********* end *********/
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class Person {
|
|
|
/********* begin *********/
|
|
|
String name;
|
|
|
int age;
|
|
|
public void a(){
|
|
|
System.out.print("姓名:"+name+","+"年龄:"+age+",");}
|
|
|
/********* end *********/
|
|
|
}
|
|
|
|
|
|
class Student extends Person {
|
|
|
/********* begin *********/
|
|
|
String school;
|
|
|
void b(){
|
|
|
super.a();
|
|
|
System.out.print("学校:"+school);}
|
|
|
/********* end *********/
|
|
|
}
|
|
|
测试输入: 无
|
|
|
预期输出:
|
|
|
我是:张三,今年:18岁,我在哈佛大学上学
|
|
|
package case4;
|
|
|
|
|
|
public class overridingTest {
|
|
|
public static void main(String[] args) {
|
|
|
// 实例化子类对象s,调用talk()方法打印信息
|
|
|
/********* begin *********/
|
|
|
|
|
|
/********* end *********/
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class Person {
|
|
|
/********* begin *********/
|
|
|
|
|
|
/********* end *********/
|
|
|
}
|
|
|
|
|
|
class Student extends Person {
|
|
|
/********* begin *********/
|
|
|
|
|
|
/********* end *********/
|
|
|
}
|
|
|
测试输入: 无
|
|
|
预期输出:
|
|
|
学生——>姓名:张三,年龄:20,职业:学生!
|
|
|
工人——>姓名:李四,年龄:30,职业:工人!
|
|
|
package case5;
|
|
|
|
|
|
public class abstractTest {
|
|
|
public static void main(String[] args) {
|
|
|
/********* begin *********/
|
|
|
// 分别实例化Student类与Worker类的对象,并调用各自构造方法初始化类属性。
|
|
|
Student s =new Student("张三",20,"学生");
|
|
|
Worker w =new Worker("李四",30,"工人");
|
|
|
s.talk();
|
|
|
w.talk();
|
|
|
// 分别调用各自类中被复写的talk()方法 打印信息。
|
|
|
|
|
|
/********* end *********/
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 声明一个名为Person的抽象类,在Person中声明了三个属性name age occupation和一个抽象方法——talk()。
|
|
|
abstract class Person {
|
|
|
/********* begin *********/
|
|
|
String name;
|
|
|
int age;
|
|
|
String occupation;
|
|
|
public abstract void talk();
|
|
|
|
|
|
/********* end *********/
|
|
|
}
|
|
|
|
|
|
// Student类继承自Person类,添加带三个参数的构造方法,复写talk()方法 返回姓名、年龄和职业信息
|
|
|
class Student extends Person {
|
|
|
/********* begin *********/
|
|
|
Student(String name,int age ,String occupation)
|
|
|
{
|
|
|
this.name=name;
|
|
|
this.age=age;
|
|
|
this.occupation=occupation;
|
|
|
}
|
|
|
public void talk(){
|
|
|
System.out.println("学生——>姓名:"+name+",年龄:"+age+",职业:"+occupation+"!");
|
|
|
}
|
|
|
/********* end *********/
|
|
|
}
|
|
|
|
|
|
// Worker类继承自Person类,添加带三个参数的构造方法,复写talk()方法 返回姓名、年龄和职业信息
|
|
|
class Worker extends Person {
|
|
|
/********* begin *********/
|
|
|
Worker(String name ,int age ,String occupation)
|
|
|
{
|
|
|
this.name=name;
|
|
|
this.age=age;
|
|
|
this.occupation=occupation;
|
|
|
}
|
|
|
public void talk()
|
|
|
{
|
|
|
System.out.println("工人——>姓名:"+name+",年龄:"+age+",职业:"+occupation+"!");
|
|
|
}
|
|
|
/********* end *********/
|
|
|
|
|
|
}
|
|
|
测试输入:无
|
|
|
预期输出:
|
|
|
speedlimit=120
|
|
|
running safely with 100kmph
|
|
|
running safely with 100kmph
|
|
|
package case6;
|
|
|
|
|
|
public class finalTest {
|
|
|
public static void main(String args[]) {
|
|
|
Bike1 obj = new Bike1();
|
|
|
obj.run();
|
|
|
|
|
|
Honda honda = new Honda();
|
|
|
honda.run();
|
|
|
|
|
|
Yamaha yamaha = new Yamaha();
|
|
|
yamaha.run();
|
|
|
}
|
|
|
}
|
|
|
//不可以修改 final 变量的值
|
|
|
// final方法,不可以重写
|
|
|
//// 不可以扩展 final 类
|
|
|
//请在此添加你的代码
|
|
|
/********** Begin *********/
|
|
|
class Bike1 {
|
|
|
int speedlimit = 90;
|
|
|
|
|
|
void run() {
|
|
|
speedlimit = 120;
|
|
|
System.out.println("speedlimit=120");
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
class Bike2 {
|
|
|
void run() {
|
|
|
System.out.println("running");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class Honda extends Bike2 {
|
|
|
void run() {
|
|
|
System.out.println("running safely with 100kmph");
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
class Bike3 {
|
|
|
}
|
|
|
|
|
|
class Yamaha extends Bike3 {
|
|
|
void run() {
|
|
|
System.out.println("running safely with 100kmph");
|
|
|
}
|
|
|
|
|
|
}
|
|
|
/********** End **********/
|
|
|
测试输入:无
|
|
|
预期输出:
|
|
|
学生——>姓名:张三,年龄:18,职业:学生!
|
|
|
package case7;
|
|
|
|
|
|
public class interfaceTest {
|
|
|
public static void main(String[] args) {
|
|
|
// 实例化一Student的对象s,并调用talk()方法,打印信息
|
|
|
/********* begin *********/
|
|
|
Student s =new Student();
|
|
|
s.talk();
|
|
|
/********* end *********/
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 声明一个Person接口,并在里面声明三个常量:name、age和occupation,并分别赋值,声明一抽象方法talk()
|
|
|
interface Person {
|
|
|
/********* begin *********/
|
|
|
String name="张三";
|
|
|
int age=18;
|
|
|
String occupation="学生";
|
|
|
/********* end *********/
|
|
|
}
|
|
|
|
|
|
// Student类继承自Person类 复写talk()方法返回姓名、年龄和职业信息
|
|
|
class Student implements Person {
|
|
|
/********* begin *********/
|
|
|
public void talk(){
|
|
|
System.out.println("学生——>姓名:"+name+",年龄:"+age+",职业:"+occupation+"!");
|
|
|
}
|
|
|
/********* end *********/
|
|
|
}
|
|
|
测试输入:无
|
|
|
预期输出:
|
|
|
eating bread...
|
|
|
eating rat...
|
|
|
eating meat...
|
|
|
package case8;
|
|
|
|
|
|
public class TestPolymorphism {
|
|
|
public static void main(String[] args) {
|
|
|
// 以多态方式分别实例化子类对象并调用eat()方法
|
|
|
/********* begin *********/
|
|
|
Dog dog = new Dog();
|
|
|
Cat cat = new Cat();
|
|
|
Lion lion = new Lion();
|
|
|
dog.eat();
|
|
|
cat.eat();
|
|
|
lion.eat();
|
|
|
/********* end *********/
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// Animal类中定义eat()方法
|
|
|
class Animal {
|
|
|
/********* begin *********/
|
|
|
public void eat(){};
|
|
|
/********* end *********/
|
|
|
}
|
|
|
|
|
|
// Dog类继承Animal类 复写eat()方法
|
|
|
class Dog extends Animal {
|
|
|
/********* begin *********/
|
|
|
public void eat(){
|
|
|
System.out.println("eating bread...");
|
|
|
}
|
|
|
/********* end *********/
|
|
|
}
|
|
|
|
|
|
// Cat类继承Animal类 复写eat()方法
|
|
|
class Cat extends Animal {
|
|
|
/********* begin *********/
|
|
|
public void eat(){
|
|
|
System.out.println("eating rat...");
|
|
|
}
|
|
|
/********* end *********/
|
|
|
}
|
|
|
|
|
|
// Lion类继承Animal类 复写eat()方法
|
|
|
class Lion extends Animal {
|
|
|
/********* begin *********/
|
|
|
public void eat(){
|
|
|
System.out.println("eating meat...");
|
|
|
}
|
|
|
/********* end *********/
|
|
|
}
|
|
|
|
|
|
Java面向对象 - 类与对象
|
|
|
测试输入:无
|
|
|
|
|
|
预期输出:使用中文冒号
|
|
|
名字:五花肉,毛色:棕色,品种:阿拉斯加
|
|
|
啃骨头
|
|
|
叼着骨头跑
|
|
|
package step1;
|
|
|
|
|
|
public class Test {
|
|
|
public static void main(String[] args) {
|
|
|
/********** Begin **********/
|
|
|
Dog wuhuarou = new Dog();//创建Dog对象
|
|
|
wuhuarou.name = "五花肉";
|
|
|
wuhuarou.color = "棕色";
|
|
|
wuhuarou.variety = "阿拉斯加";
|
|
|
//设置Dog对象的属性
|
|
|
//输出小狗的属性
|
|
|
System.out.println("名字:" + wuhuarou.name + ",毛色:" +wuhuarou.color + ",品种:" + wuhuarou.variety );
|
|
|
wuhuarou.eat();
|
|
|
wuhuarou.run();
|
|
|
//调用方法
|
|
|
/********** End **********/
|
|
|
}
|
|
|
}
|
|
|
//在这里定义Dog类
|
|
|
/********** Begin **********/
|
|
|
class Dog{
|
|
|
String name;
|
|
|
String color;
|
|
|
String variety;
|
|
|
public void eat(){
|
|
|
System.out.println("啃骨头"); }
|
|
|
public void run(){
|
|
|
System.out.println("叼着骨头跑"); }}
|
|
|
测试输入:张富贵,未知
|
|
|
预期输出:
|
|
|
一个人被创建了
|
|
|
姓名:张富贵,性别:未知,被创建了
|
|
|
package step2;
|
|
|
import java.util.Scanner;
|
|
|
public class Test {
|
|
|
public static void main(String[] args) {
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
String name = sc.next();
|
|
|
String sex = sc.next();
|
|
|
/********** Begin **********/
|
|
|
//分别使用两种构造器来创建Person对象
|
|
|
Person sut = new Person();
|
|
|
Person sut1 = new Person(name,sex);
|
|
|
/********** End **********/}}
|
|
|
|
|
|
//创建Person对象,并创建两种构造方法
|
|
|
/********** Begin **********/
|
|
|
class Person{
|
|
|
public Person(){
|
|
|
System.out.println("一个人被创建了"); }
|
|
|
public Person(String stuname,String stusex){
|
|
|
System.out.println("姓名:"+stuname+",性别:"+stusex+",被创建了"); }}
|
|
|
测试输入:
|
|
|
baby,45,女
|
|
|
预期输出:
|
|
|
name:baby
|
|
|
age:45
|
|
|
sex:女
|
|
|
package step3;
|
|
|
import java.util.Scanner;
|
|
|
public class Test {
|
|
|
public static void main(String[] args) {
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
String name = sc.next();
|
|
|
int age = sc.nextInt();
|
|
|
String sex = sc.next();
|
|
|
Person p = new Person(name,age,sex);
|
|
|
p.display();}}
|
|
|
class Person{
|
|
|
String name = "张三";
|
|
|
int age = 18;
|
|
|
String sex = "男";
|
|
|
/********** Begin **********/
|
|
|
public Person(String name,int age,String sex){
|
|
|
this.age = age;
|
|
|
this.name = name;
|
|
|
this.sex = sex;}
|
|
|
public Person(int age){
|
|
|
this.age = age;}
|
|
|
public void display(){
|
|
|
System.out.println("name:" + name);
|
|
|
System.out.println("age:" + age);
|
|
|
System.out.println("sex:" + sex); }
|
|
|
}
|
|
|
/********** End **********/
|
|
|
测试输入:
|
|
|
牛肉,3,true
|
|
|
预期输出:
|
|
|
面码:牛肉,粉的份量:3两,是否带汤:true
|
|
|
面码:牛肉,粉的份量:3两,是否带汤:false
|
|
|
面码:酸辣,粉的份量:2两,是否带汤:true
|
|
|
测试输入:
|
|
|
三鲜,1,true
|
|
|
预期输出:
|
|
|
面码:三鲜,粉的份量:1两,是否带汤:true
|
|
|
面码:三鲜,粉的份量:1两,是否带汤:false
|
|
|
面码:酸辣,粉的份量:2两,是否带汤:true
|
|
|
package step4;
|
|
|
import java.util.Scanner;
|
|
|
public class Test {
|
|
|
public static void main(String[] args) {
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
String theMa = sc.next();
|
|
|
int quantity = sc.nextInt();
|
|
|
boolean likeSoup = sc.nextBoolean();
|
|
|
/********** Begin **********/
|
|
|
WuMingFen f1 = new WuMingFen(theMa,quantity,likeSoup);//使用三个参数的构造方法创建WuMingFen对象 取名 f1
|
|
|
WuMingFen f2= new WuMingFen(theMa,quantity);//使用两个参数的构造方法创建WuMingFen对象 取名 f2
|
|
|
WuMingFen f3 = new WuMingFen();//使用无参构造方法创建WuMingFen对象 取名 f3
|
|
|
f1.check();//分别调用三个类的 check方法
|
|
|
/********** End **********/
|
|
|
}
|
|
|
}
|
|
|
package step4;//在这里添加包名 step4
|
|
|
|
|
|
//创建类 添加属性和方法
|
|
|
public class WuMingFen{
|
|
|
String TheMa;
|
|
|
int quantity;
|
|
|
boolean likeSoup;
|
|
|
public WuMingFen(String TheMa,int quantity,boolean likeSoup){
|
|
|
this.TheMa = TheMa;
|
|
|
this.quantity = quantity;
|
|
|
this.likeSoup = likeSoup;
|
|
|
}
|
|
|
public WuMingFen(String TheMa,int quantity){
|
|
|
this.TheMa = TheMa;
|
|
|
this.quantity = quantity;
|
|
|
likeSoup = true;
|
|
|
}
|
|
|
public WuMingFen(){
|
|
|
likeSoup = false;
|
|
|
}
|
|
|
public void check(){
|
|
|
System.out.println("面码:"+TheMa+",粉的份量:"+quantity+"两,是否带汤:"+likeSoup);
|
|
|
likeSoup = false;
|
|
|
System.out.println("面码:"+TheMa+",粉的份量:"+quantity+"两,是否带汤:"+likeSoup);
|
|
|
likeSoup = true;
|
|
|
System.out.println("面码:酸辣,粉的份量:2两,是否带汤:"+likeSoup);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
hello educoder
|
|
|
我叫楚留香
|
|
|
我喜欢在educoder上学习java
|
|
|
package step5;
|
|
|
|
|
|
public class Test {
|
|
|
/********** Begin **********/
|
|
|
static String name = "楚留香";
|
|
|
|
|
|
static{
|
|
|
System.out.println("hello educoder");
|
|
|
}
|
|
|
public static void main(String[] args) {
|
|
|
System.out.println("我叫"+name);
|
|
|
study();
|
|
|
}
|
|
|
|
|
|
public static void study(){
|
|
|
System.out.println("我喜欢在educoder上学习java");
|
|
|
}
|
|
|
/********** End **********/
|
|
|
}
|
|
|
数组进阶
|
|
|
|
|
|
预期输入:6,9,5,8,0,2,6
|
|
|
预期输出:[0, 2, 5, 6, 8, 9]
|
|
|
|
|
|
预期输入:4,3,12,13,0
|
|
|
预期输出:[0, 3, 12, 13]
|
|
|
package step5;
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
public class HelloWorld {
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
//动态创建数组
|
|
|
int[] arr = new int[sc.nextInt()];
|
|
|
for(int i = 0 ; i< arr.length ; i++){
|
|
|
arr[i] = sc.nextInt();
|
|
|
}
|
|
|
/********** Begin **********/
|
|
|
for(int i=0;i<arr.length;i++){
|
|
|
for(int j=0;j<arr.length-i-1;j++){
|
|
|
if(arr[j]>arr[j+1]){
|
|
|
int temp=arr[j];
|
|
|
arr[j]=arr[j+1];
|
|
|
arr[j+1]=temp;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
System.out.println(Arrays.toString(arr));
|
|
|
/********** End **********/
|
|
|
}
|
|
|
}
|
|
|
我会对你的程序进行测试;
|
|
|
预期输入:6,6,5,0,2,8,9
|
|
|
预期输出:[9, 8, 6, 5, 2, 0]
|
|
|
|
|
|
预期输入:4,3,12,1,13
|
|
|
预期输出:[13, 12, 3, 1]
|
|
|
package step4;
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
public class HelloWorld {
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
//动态创建数组
|
|
|
int[] arr = new int[sc.nextInt()];
|
|
|
for(int i = 0 ; i< arr.length ; i++){
|
|
|
arr[i] = sc.nextInt();
|
|
|
}
|
|
|
/********** Begin **********/
|
|
|
int max=0;
|
|
|
int ch=-1;
|
|
|
int[] a=new int[arr.length];
|
|
|
for(int i=0;i<arr.length;i++){
|
|
|
for(int j=0;j<arr.length;j++){
|
|
|
if(arr[j]>max){
|
|
|
max=arr[j];
|
|
|
ch=j;
|
|
|
}
|
|
|
}
|
|
|
arr[ch]=0;
|
|
|
a[i]=max;
|
|
|
max=-1;
|
|
|
ch=-1;
|
|
|
}
|
|
|
System.out.println(Arrays.toString(a));
|
|
|
/********** End **********/
|
|
|
}
|
|
|
}
|
|
|
测试输入:3,5
|
|
|
预期输出:
|
|
|
5
|
|
|
3
|
|
|
测试输入:4,2
|
|
|
预期输出:
|
|
|
2
|
|
|
4
|
|
|
package step3;
|
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
public class HelloWorld {
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
int a = sc.nextInt();
|
|
|
int b = sc.nextInt();
|
|
|
/********** Begin **********/
|
|
|
//将a的值赋给b b的值赋给a
|
|
|
int t=a;
|
|
|
a=b;
|
|
|
b=t;
|
|
|
/********** End **********/
|
|
|
System.out.println(a);
|
|
|
System.out.println(b);
|
|
|
}
|
|
|
}
|
|
|
测试输入:张三
|
|
|
预期输出:张三在数组的第1个位置
|
|
|
|
|
|
测试输入:张富贵
|
|
|
预期输出:张富贵在数组的第5个位置
|
|
|
package step2;
|
|
|
import java.util.Scanner;
|
|
|
public class HelloWorld {
|
|
|
public static void main(String[] args) {
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
//str为要查找的字符串
|
|
|
String str = sc.next();
|
|
|
/********** Begin **********/
|
|
|
//创建数组 arr 给数组赋值 {"张三","张三丰","张无忌","王二麻子","张富贵"}
|
|
|
String[] arr = {"张三","张三丰","张无忌","王二麻子","张富贵"};
|
|
|
for(int i=0;i<5;i++){
|
|
|
if(str.equals(arr[i])){
|
|
|
System.out.println( str+ "在数组的第" + (i+1) + "个位置" );
|
|
|
}
|
|
|
}
|
|
|
/********** End **********/
|
|
|
}
|
|
|
}
|
|
|
测试输入:1,3,12
|
|
|
预期输出:
|
|
|
1
|
|
|
3
|
|
|
12
|
|
|
测试输入:2,13,11
|
|
|
预期输出:
|
|
|
2
|
|
|
13
|
|
|
11
|
|
|
package step1;
|
|
|
import java.util.Scanner;
|
|
|
public class HelloWorld {
|
|
|
public static void main(String[] args) {
|
|
|
//动态构建arr1
|
|
|
int[] arr1 = new int[3];
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
for(int i = 0 ; i< arr1.length ; i++){
|
|
|
arr1[i] = sc.nextInt();
|
|
|
}
|
|
|
/********** Begin **********/
|
|
|
//创建数组arr2
|
|
|
int[]arr2=new int[3];
|
|
|
|
|
|
//使用for循环将arr1的数据复制给arr2
|
|
|
for(int i=0;i<3;i++){
|
|
|
arr2[i]=arr1[i];
|
|
|
}
|
|
|
//输出arr2
|
|
|
for(int i=0;i<3;i++){
|
|
|
System.out.println(arr2[i]);
|
|
|
}
|
|
|
/********** End **********/
|
|
|
}
|
|
|
}
|
|
|
|