学习视频链接:https://www.bilibili.com/video/BV17F411T7Ao
需求:
分析以下需求用代码实现:
定义形状类:
功能:求面积,求周长
定义圆形类Round:
属性:半径,圆周率
功能:求面积,求周长
定义长方形类Rectangle:
属性:长和宽
功能:求面积,求周长
定义测试类:
分别创建圆形和长方形对象,为相应的属性赋值
分别调用求面积和求周长的方法
package cn.kox.homework01;
/**
* @ClassName: Graph
* @Author: Kox
* @Data: 2023/1/29
* @Sketch:
*/
public abstract class Graph {
// 面积
public abstract void area();
// 周长
public abstract void perimeter();
}
package cn.kox.homework01;
/**
* @ClassName: Rectangle
* @Author: Kox
* @Data: 2023/1/29
* @Sketch:
*/
public class Rectangle extends Graph{
private double length;
private double wide;
public Rectangle() {
}
public Rectangle(double length, double wide) {
this.length = length;
this.wide = wide;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWide() {
return wide;
}
public void setWide(double wide) {
this.wide = wide;
}
@Override
public void area() {
double result = length * wide;
System.out.println("长方形的面积为:" + result);
}
@Override
public void perimeter() {
double result = (length + wide) * 2;
System.out.println("长方形的周长为:" + result);
}
}
package cn.kox.homework01;
/**
* @ClassName: Round
* @Author: Kox
* @Data: 2023/1/29
* @Sketch:
*/
public class Round extends Graph{
private double radius;
private double PI;
public Round() {
}
public Round(double radius, double PI) {
this.radius = radius;
this.PI = PI;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getPI() {
return PI;
}
public void setPI(double PI) {
this.PI = PI;
}
@Override
public void area() {
double result = radius * PI;
System.out.println("圆面积为:" + result);
}
@Override
public void perimeter() {
double result = 2 * PI * radius;
System.out.println("圆周长为:" + result);
}
}
package cn.kox.homework01;
/**
* @ClassName: Test
* @Author: Kox
* @Data: 2023/1/29
* @Sketch:
*/
public class Test {
public static void main(String[] args) {
Round r = new Round(2, 3.14);
r.area();
r.perimeter();
Rectangle r1 = new Rectangle(10, 5);
r1.area();
r1.perimeter();
}
}
需求:
行为:打电话,发短信
行为:玩游戏
行为:继承父类的行为
定义新手机继承手机类实现IPlay接口
行为:继承父类的行为,重写玩游戏方法
定义测试类
在测试类中定义一个 用手机的方法,要求该方法既能接收老手机对象,也能接收新手机对象
在该方法内部调用打电话,发短信以及新手机特有的玩游戏方法
package cn.kox.homework02;
/**
* @ClassName: formerPhone
* @Author: Kox
* @Data: 2023/1/29
* @Sketch:
*/
public class formerPhone extends Phone{
public void show() {
System.out.println("老手机");
}
}
package cn.kox.homework02;
public interface IPlay {
void playGame();
}
package cn.kox.homework02;
/**
* @ClassName: newPhone
* @Author: Kox
* @Data: 2023/1/29
* @Sketch:
*/
public class newPhone extends Phone implements IPlay{
@Override
public void playGame() {
System.out.println("玩游戏");
}
public void show() {
System.out.println("新手机");
}
}
package cn.kox.homework02;
/**
* @ClassName: Phone
* @Author: Kox
* @Data: 2023/1/29
* @Sketch:
*/
public class Phone {
public void send() {
System.out.println("发短信");
}
public void phoning() {
System.out.println("打电话");
}
}
package cn.kox.homework02;
/**
* @ClassName: Test
* @Author: Kox
* @Data: 2023/1/29
* @Sketch:
*/
public class Test {
public static void main(String[] args) {
formerPhone f = new formerPhone();
f.show();
f.phoning();
f.send();
System.out.println();
newPhone n = new newPhone();
n.show();
n.phoning();
n.send();
}
}
需求:
要求1.创建子类实现接口的方式实现
要求2:用匿名内部类实现
要求1.创建子类继承抽象类的方式实现
要求2:用匿名内部类实现
package cn.kox.homework03;
public interface Fun {
public abstract void fun();
}
package cn.kox.homework03;
/**
* @ClassName: funSon
* @Author: Kox
* @Data: 2023/1/29
* @Sketch:
*/
public class funSon implements Fun{
@Override
public void fun() {
System.out.println("开个玩笑吧");
}
}
package cn.kox.homework03;
public interface IPlay {
void playGame();
}
package cn.kox.homework03;
/**
* @ClassName: Test
* @Author: Kox
* @Data: 2023/1/29
* @Sketch:
*/
public class Test {
public static void main(String[] args) {
IPlay i = new IPlay() {
@Override
public void playGame() {
System.out.println("打游戏");
}
};
i.playGame();
funSon f = new funSon();
f.fun();
}
}