黑马程序员技术交流社区
标题:
java基础9-22预习
[打印本页]
作者:
jekyll
时间:
2015-9-21 18:54
标题:
java基础9-22预习
class review9_22 {
public static void main(String[] args) {
//代码块
//ARea area = new ARea();
//继承动物类
//Cat miao = new Cat("白",4);
//miao.catchMouse();
//继承类实例化过程
//Zi z = new Zi();
//重写手机类
//Nokia nokia = new Nokia("Nokia",199);
//nokia.call();
/*
* A:案例演示
* 属性:姓名,年龄
* 行为:吃饭
* 老师有特有的方法:讲课
* 学生有特有的方法:学习
*/
/*Teacher t = new Teacher();
Student s = new Student();
t.teach();
s.study();
*/
}
}
class Person {
private String name;
private int age;
Person() {}
void eat() {
System.out.println("吃饭");
}
}
class Teacher extends Person {
void teach() {
System.out.println("教课");
}
}
class Student extends Person {
void study() {
System.out.println("学习");
}
}
class MobilePhone {
private String brand;
private int price;
MobilePhone(String brand,int price) {
this.brand = brand;
this.price = price;
}
public String getBrand() {
return this.brand;
}
public int getprice(){
return this.price;
}
public void setprice(int price){
this.price = price;
}
void call() {
System.out.println("用"+this.brand+"打电话");
}
public void sendMessage(Student stu) {
System.out.println("用"+this.brand+"发短信");
}
public void playGame() {
System.out.println("用"+this.price+"的手机玩游戏");
}
}
class Nokia extends MobilePhone {
Nokia(String brand,int price) {
super(brand,price);
}
void call() {
System.out.println("用"+getBrand()+"砸核桃");
}
}
class Fu {
static {
System.out.println("静态代码块Fu");
}
{
System.out.println("构造代码块Fu");
}
public Fu() {
System.out.println("构造方法Fu");
}
}
class Zi extends Fu {
static {
System.out.println("静态代码块Zi");
}
{
System.out.println("构造代码块Zi");
}
public Zi() {
System.out.println("构造方法Zi");
}
}
/*
* 动物类,猫类,狗类
* 定义两个属性(颜色,腿的个数)两个功能(吃饭,睡觉)
*/
class Animal {
String color;
int leg;
Animal() {};
void eat() {
System.out.println("吃饭");
}
void sleep() {
System.out.println("睡觉");
}
}
class Cat extends Animal {
Cat(String color,int leg) {
this.color = color;
this.leg = leg;
}
void catchMouse() {
System.out.println(this.color+"喵捉老鼠");
}
}
class Dog extends Animal {
Dog(String color,int leg) {
this.color = color;
this.leg = leg;
}
void lookHome() {
System.out.println(this.color+"看家");
}
}
class ARea {
static {
System.out.println("静态代码块在方法区静态域加载,初始化类");
}
{
System.out.println("构造代码块");
}
ARea() {
System.out.println("构造方法");
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2