定义Person类,变量name,ID。 用户不能直接修改这些变量,但可以通过getName,setName方法获取Name 属性和设置Name属性,ID也是如此访问。// Person.java
Java代码:
package third_first_second;
public class Person {
private char name;
private char ID;
public char getName() {
return this.name;
}
public char getID() {
return this.ID;
}
public void setName(char name) {
this.name = name;
}
public void setID(char ID) {
this.ID = ID;
}
public void eat() {
System.out.println("大多数人用筷子或刀叉吃饭");
}
}
定义Person类的eat()方法。定义American类,和Chinese类,分别继承Person 类,并重写Person 类的eat方法。American的eat方法输出“我用刀叉吃西餐”。Chinese 的eat方法输出”我用筷子吃中餐”。并用测试类调用测试。 //American.java, Chinese.java
Java代码:
package third_first_second;
public class Chinese extends Person{
public void eat() {
System.out.println("我用筷子吃中餐");
}
}
package third_first_second;
public class American extends Person{
public void eat() {
System.out.println("我用刀叉吃西餐");
}
}
package third_first_second;
public class Demo_one {
public static void main(String args[]) {
Person person = null;
person = new Chinese();
person.eat();
person = new American();
person.eat();
}
}
package third_first_second;
public class Demo_two {
public static void main(String[] args) {
Chinese chinese = new Chinese();
American american = new American();
chinese.eat();
american.eat();
}
}
设计一个汽车类Auto,其有速度属性speed,以及启动start、加速speedup和停止stop方法,从Auto类派生一个子类Bus,以表示公共汽车,且Bus增加一个表示乘客数的属性passenger,另外添连个表示乘客上车和下车的方法getOn和getOff. //Auto.java, Bus.java
java代码:
package third_third;
public abstract class Auto {
int speed;
int start;
int speedup;
abstract void stop();
}
package third_third;
public abstract class Bus extends Auto{
int passenger;
abstract void getOn();
abstract void getOff();
}
P88的实验2
Java代码:
package third_fourth_first;
public class School {
Teacher mathTeacher, musicTeacher;
void setTeacher(Teacher t1, Teacher t2) {
mathTeacher = t1;
musicTeacher = t2;
}
void startMathLesson() {
mathTeacher.speak();
}
void startMusicLesson() {
musicTeacher.speak();
}
}
package third_fourth_first;
public class Teacher {
int teacherType;
public void speak() {
if(teacherType == 1) {
System.out.println("课程的内容是二次方程");
}
else if(teacherType == 2) {
System.out.println("课程的内容是学唱五线谱");
}
}
}
package third_fourth_first;
public class MainClass {
public static void main(String[] args) {
Teacher zhang, wang;
zhang = new Teacher();
wang = new Teacher();
zhang.teacherType = 1;
wang.teacherType = 2;
School 实验中学 = new School();
实验中学.setTeacher(zhang, wang);
实验中学.startMathLesson();
实验中学.startMusicLesson();
}
}
p89实验三
Java代码:
package third_fourth_second;
public class Village {
static int treeAmout;
int peopleNumber;
String name;
Village(String s){
name = s;
}
void treePlanting(int n) {
treeAmout = treeAmout + n;
System.out.println(name + "植树" + n + "颗");
}
void fellTree(int n) {
if(treeAmout - n > 0) {
treeAmout = treeAmout - n;
System.out.println(name + "伐树" + n + "颗");
}
else {
System.out.println("无树木可伐");
}
}
static int lookTreeAmout() {
return treeAmout;
}
void addPeopleNumber(int n) {
peopleNumber = peopleNumber + n;
System.out.println(name + "增加了" + n + "人");
}
}
package third_fourth_second;
public class MainClass {
public static void main(String[] args) {
Village zhaoZhuang, maJiaHeZhi;
zhaoZhuang = new Village("赵庄");
maJiaHeZhi = new Village("马家河子");
zhaoZhuang.peopleNumber = 100;
maJiaHeZhi.peopleNumber = 150;
Village.treeAmout = 200;//通过类名调用类变量并给赋初值
int leftTree = Village.treeAmout;
System.out.println("森林中有 " + leftTree + " 颗树");
zhaoZhuang.treePlanting(50);
leftTree = maJiaHeZhi.lookTreeAmout();//用对象访问类变量
System.out.println("森林中有 " + leftTree + " 棵树");
maJiaHeZhi.fellTree(70);
leftTree = Village.treeAmout;//用类名访问类变量
System.out.println("森林中有 " + leftTree + " 棵树");
System.out.println("赵庄的人口:" + zhaoZhuang.peopleNumber);
zhaoZhuang.addPeopleNumber(12);
System.out.println("赵庄的人口:" + zhaoZhuang.peopleNumber);
System.out.println("马家河子的人口:" + maJiaHeZhi.peopleNumber);
maJiaHeZhi.addPeopleNumber(10);
System.out.println("马家河子的人口:" + maJiaHeZhi.peopleNumber);
}
}
p118实验1.
Java代码:
package third_fourth_third;
public class Animal {
protected String name;
public void showName() {
System.out.println(name);
}
public void cry() {
System.out.println("不同动物的叫声是有区别的");
}
}
package third_fourth_third;
public class Dog extends Animal{
Dog(String s){
name = s;
}
public void cry() {
System.out.println("汪汪汪");
}
public void swimming() {
System.out.println(name + "会游泳");
}
}
package third_fourth_third;
public class Cat extends Animal{
Cat(){
name = "猫";
}
Cat(String s){
name = s;
}
public void cry() {
System.out.println("喵喵喵");
}
public void climbUpTree() {
System.out.println(name + "会爬树");
}
}
package third_fourth_third;
public class MainClass {
public static void main(String[] args) {
Dog dog = new Dog("小黄狗");
Cat cat = new Cat("小花猫");
dog.showName();
dog.cry();
dog.swimming();
cat.showName();
cat.cry();
cat.climbUpTree();
}
}