Initial commit

This commit is contained in:
2025-11-06 10:29:13 +08:00
commit 0becd14830
318 changed files with 7145 additions and 0 deletions

View 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();
}
}

View 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);
}
}

View 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);
}
}

View 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());
}
}

View 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个用户用户有用户名密码属性登录注册动作、方法