Initial commit
This commit is contained in:
44
实验报告模板/实验报告4/BackAnimal.java
Normal file
44
实验报告模板/实验报告4/BackAnimal.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package ch04;
|
||||
|
||||
abstract class Animal{
|
||||
public abstract void eat(); //只有方法名,而没有方法体
|
||||
}
|
||||
class Dog extends Animal{
|
||||
private String name;
|
||||
public void setName(String name){
|
||||
this.name=name;
|
||||
}
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
public void eat(){
|
||||
System.out.println("小狗啃骨头啦");
|
||||
}
|
||||
}
|
||||
class Bird extends Animal{
|
||||
public void fly( ){
|
||||
System.out.println("小鸟飞呀飞呀");
|
||||
}
|
||||
public void eat( ){
|
||||
System.out.println("小鸟吃虫子啦");
|
||||
}
|
||||
}
|
||||
public class BackAnimal{
|
||||
public static void main( String args[ ] ){
|
||||
Animal a1=new Animal(); //1.该行代码可不可以执行?为什么?
|
||||
Animal an = new Dog( );
|
||||
an.eat( ); //2.该行代码输出什么?
|
||||
an=new Bird();
|
||||
an.eat(); //3.该行代码输出什么?
|
||||
Animal x1=new Dog();
|
||||
Animal x2=new Bird();
|
||||
print(x1); //4.以下2行代码输出什么?为什么?
|
||||
print(x2);
|
||||
|
||||
}
|
||||
public static void print(Animal a){
|
||||
a.eat();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
28
实验报告模板/实验报告4/ex4-2023.11.01版第4题分析/Sobject.java
Normal file
28
实验报告模板/实验报告4/ex4-2023.11.01版第4题分析/Sobject.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package com.ex4;
|
||||
|
||||
public class Sobject {
|
||||
private String name;
|
||||
private double weight; //ÖØÁ¿
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public double getWeight() {
|
||||
return weight;
|
||||
}
|
||||
public void setWeight(double weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
public Sobject(String name, double weight) {
|
||||
this.name = name;
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public void print() {
|
||||
System.out.println("name:"+name+",weight:"+weight);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
28
实验报告模板/实验报告4/ex4-2023.11.01版第4题分析/Table.java
Normal file
28
实验报告模板/实验报告4/ex4-2023.11.01版第4题分析/Table.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package com.ex4;
|
||||
|
||||
public class Table extends Sobject{
|
||||
private double width; //宽度
|
||||
private double length; //长度
|
||||
private double height; //高度
|
||||
|
||||
//如果该类不写构造方法,系统会自动为 Table添加一个无参数的构造方法
|
||||
//在这个无参构造方法中,会自动调用Sobject类中的无参数的构造方法
|
||||
// public Table() {
|
||||
// super();
|
||||
//// super("abc",34.5);
|
||||
// }
|
||||
//
|
||||
public Table(String name,double weight,double width,double length,double height) {
|
||||
super(name, weight);
|
||||
this.width=width;
|
||||
this.length=length;
|
||||
this.height=height;
|
||||
|
||||
}
|
||||
public double area() {
|
||||
return width*length;
|
||||
}
|
||||
public void changeWeight(double w) {
|
||||
setWeight(w);
|
||||
}
|
||||
}
|
||||
15
实验报告模板/实验报告4/ex4-2023.11.01版第4题分析/Test.java
Normal file
15
实验报告模板/实验报告4/ex4-2023.11.01版第4题分析/Test.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.ex4;
|
||||
|
||||
public class Test {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
Table table=new Table("A1", 23, 1.8, 2.3, 0.8);
|
||||
double area=table.area();
|
||||
table.changeWeight(45);
|
||||
System.out.println(area);
|
||||
System.out.println(table.getWeight());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
BIN
实验报告模板/实验报告4/实验报告模板-实验4 - 2023.11.01.doc
Normal file
BIN
实验报告模板/实验报告4/实验报告模板-实验4 - 2023.11.01.doc
Normal file
Binary file not shown.
BIN
实验报告模板/实验报告4/实验报告模板-实验4.doc
Normal file
BIN
实验报告模板/实验报告4/实验报告模板-实验4.doc
Normal file
Binary file not shown.
18
实验报告模板/实验报告4/实验讲课.txt
Normal file
18
实验报告模板/实验报告4/实验讲课.txt
Normal file
@@ -0,0 +1,18 @@
|
||||
接口
|
||||
若类中的所有的方法都是抽象方法,并且没有成员变量----->接口
|
||||
|
||||
抽象类:可以有成员变量、可以有普通方法、可以有抽象方法
|
||||
接口:
|
||||
(1)没有成员变量,只有常量与抽象方法,接口中定义的所有方法都是抽象方法
|
||||
(2)即接口不可以直接创建象,接口必须通过派生子类来创建对象
|
||||
(3)接口中所有方法都必须在子类中重写。
|
||||
(4)接口中可以定义常量
|
||||
int footnum=2;<===>public final static int footnum=2;
|
||||
(5)接口可以多继承,1个类可以继承多个接口,但1个类只能继承1个父类
|
||||
|
||||
final,staic
|
||||
public final static int footernum=-1; //定义了静态、常量,可以通过类名调用
|
||||
public static int footernum=-1; //静态常量,该变量可以被类创建的所有对象共享,可以通过类名调用
|
||||
public final int footernum=-1; //常量,该常量,必须通过对象名来调用
|
||||
|
||||
设计1个用户,用户有用户名,密码属性,登录,注册(动作、方法)
|
||||
Reference in New Issue
Block a user