1.
* 练习:
* 自定义抽象类Account实现封装,特征:账户余额(balance),在该类中提供一个计算利息并返回的抽象方法。
* 自定义FixedAccount类继承Account类并重写抽象方法,
* 要求在main方法中使用多态方式构造对象并存入1000元,计算利息后打印出来(其中利率0.003,时间1年)。
package com.java.account;
public abstract class Account {
private double balance;
/**
* 计算利息的方法
* @return
*/
public abstract double getInterest();
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Account(double balance) {
this.balance = balance;
}
public Account() {
}
}
package com.java.account;
public class FixedAccount extends Account{
@Override
public double getInterest() {
return 0;
}
}
2.
package com.java.animal;
* 练习:
* 编程实现Animal类的封装,特征有:名字和毛色,要求提供打印所有特征的方法。
* 编程实现Dog类的封装并继承自Animal类,该类的特征有:牙齿数量,要求提供打印所有特征的方法
* 编程实现DogTest类,在main方法中分别使用无参和有参方式构造Dog类型对象并打印特征。
public class Animal {
private String name;
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Animal(String name, String color) {
this.name = name;
this.color = color;
}
public Animal() {
}
public void show(){
System.out.println("我的名字叫做" + getName() + ",我的颜色是" + getColor() + "色");
}
}
package com.java.animal;
public class Dog extends Animal{
private int teethNumber;
public int getTeethNumber() {
return teethNumber;
}
public void setTeethNumber(int teethNumber) {
this.teethNumber = teethNumber;
}
public Dog(String name, String color, int teethNumber) {
super(name, color);
this.teethNumber = teethNumber;
}
public Dog(int teethNumber) {
this.teethNumber = teethNumber;
}
public Dog() {
}
@Override
public void show(){
System.out.println("我叫" + getName() + ",颜色是" + getColor() + "牙齿数量是:" + getTeethNumber());
}
}
package com.java.animal;
public class DogTest {
public static void main(String[] args) {
Dog dog = new Dog();
dog.show();
Dog dog1 = new Dog("亚里","粉色",1);
dog1.show();
}
}
3. final 修饰 属性(成员变量)、方法、类
package com.java.finaltest;
public class A {
public final void show(){
}
}
package com.java.finaltest;
public class B extends A{
}
package com.java.finaltest;
public class FinalDemo {
public static final String SQL_INSERT = "";
final int age = 0;
public static void main(String[] args) {
final int a;
a = 0;
}
}
4. 练习
编程实现Person类的封装,特征有:姓名和年龄,要求提供打印所有特征的方法。
编程实现Worker类的封装并继承自Person类,特征有:薪水
编程实现PersonWorkerTest类,在main方法中分别创建Person和Worker类型对象并打印特征。
编程实现Person类型的引用指向Worker类型的对象,并打印特征。
package com.java.person;
public abstract class Nurse extends Person{
@Override
public void say() {
}
public abstract void skill();
}
package com.java.person;
public abstract class Person {
String name;
int age;
double height;
public Person() {
System.out.println("Person无参构造被调用了...");
}
public void show(){
System.out.println("我是Person...show()");
}
public abstract void say();
}
package com.java.person;
public class PersonTest {
public static void main(String[] args) {
//父类类型 引用变量名 = new 子类类型();
Person person = new Student();
person.show(); //在编译阶段调用Person的show方法,在运行阶段,调用的是Student中的show方法
}
}
package com.java.person;
public class Student extends Person{
public Student() {
System.out.println("Student 无参构造被调用了...");
}
@Override
public void show(){
System.out.println("我是Student...show()");
}
@Override
public void say() {
}
}
5.接口
package com.java.run;
public interface Hunter extends Runner{
void hunt();
}
package com.java.run;
public interface Runner {
void running();
}
package com.java.run;
public class Chinese implements Hunter{
@Override
public void hunt() {
System.out.println("抓到一只小白兔");
}
@Override
public void running() {
System.out.println("正在全力的奔跑");
}
}
package com.java.run;
public class ChineseTest {
public static void main(String[] args) {
//接口类型的引用指向实现类的对象,行程多态
Runner runner = new Chinese();
runner.running();
Hunter hunter = new Chinese();
hunter.hunt();
}
6. 练习 自定义ShapeTest类,自定义成员方法实现既能打印矩形又能打印圆形的方法并调用
package com.java.shape;
public class Circle extends Shape {
private double radius;
@Override
public void show() {
System.out.println("[ x = " + getX() + ",y = " + getY() + ",radius = " + getRadius() + "]");
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
public Circle(int x, int y, double radius) {
super(x, y);
this.radius = radius;
}
}
package com.java.shape;
public class Rect extends Shape{
private int lenth;
private int width;
@Override
public void show() {
System.out.println("[x =" + getX() + ",y = "+ getY() + ",length = " + getLenth() + ",width = " + getWidth() + "]");
}
public int getLenth() {
return lenth;
}
public void setLenth(int lenth) {
this.lenth = lenth;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public Rect() {
}
public Rect(int lenth, int width) {
this.lenth = lenth;
this.width = width;
}
public Rect(int x, int y, int lenth, int width) {
super(x, y);
this.lenth = lenth;
this.width = width;
}
}
package com.java.shape;
public class Shape {
private int x;
private int y;
public void show(){
System.out.println("[x = " + getX() + ",y = " + getY() + "]");
}
public Shape() {
}
public Shape(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
package com.java.shape;
public class ShapeTest {
/*public static void printRect(Rect rect){
rect.show();
}
public static void printCircle(Circle circle){
circle.show();
}*/
//既能打印矩形又能打印圆形
public static void printShape(Shape shape){
shape.show();
}
//static 修饰的方法只能调用静态的方法
public static void main(String[] args) {
/*Shape shape = new Shape(3,4);
shape.show();
Rect rect = new Rect(1,2,3,4);
rect.show();
Circle circle = new Circle(1,2,4);
circle.show();
printRect(new Rect(1,2,3,4));
printCircle(new Circle(1,2,4));
//父类型 引用变量名 = new 子类类型();
Shape shape = new Rect(1,2,3,4);
shape.show();
Shape shape1 = new Circle(1,2,4);
shape1.show();*/
printShape(new Circle(1,2,4));
}
}
7.继承
package com.java.yemage;
public class Person {
public Person() {
System.out.println("父类无参构造方法");
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
private String name;
private int age;
public void eat(){
}
public void play(){}
}
package com.java.yemage;
public class PersonTest {
public static void main(String[] args) {
Student student = new Student();
}
}
package com.java.yemage;
public class Student extends Person{
public Student() {
super();
}
public Student(int studentId) {
this.studentId = studentId;
}
public Student(String name, int age, int studentId) {
super(name, age);
this.studentId = studentId;
}
private int studentId;
public void study(){}
}
package com.java.yemage;
public class Teacher extends Person{
private int teacherId;
public void teaching(){
}
}
package com.java.yemage;
public class Worker extends Person{
private double salary;
public void working(){
}
}