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,42 @@
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 an = new Dog();
an.eat();
an = new Bird();
an.eat();
}
}

View File

@@ -0,0 +1,31 @@
package ch04;
class 类人猿 {
public 类人猿() {
System.out.println("类人猿 Created!");
}
void crySpeak() {
System.out.println(" 我是类人猿。");
}
}
class People1 extends 类人猿 {
public People1() {
System.out.println("People Created!");
}
void crySpeak() {
System.out.println(" 我是People。");
}
}
class BackwardObject {
public static void main(String[] args) {
类人猿 originalPeople = new 类人猿();
People1 people = new People1();
originalPeople = people;
originalPeople.crySpeak();
people.crySpeak();
}
}

View File

@@ -0,0 +1,41 @@
package ch04;
class Sum {
int n;
float f() {
float sum = 0;
for (int i = 1; i <= n; i++)
sum = sum + i;
return sum;
}
}
class Average extends Sum {
int n;
float f() {
float c;
super.n = n;
c = super.f();
return c / n;
}
float g() {
float c;
c = super.f();
return c / 2;
}
}
public class CallHidenVar {
public static void main(String[] args) {
Average aver = new Average();
aver.n = 100;
float result_1 = aver.f();
float result_2 = aver.g();
System.out.println("result_1=" + result_1);
System.out.println("result_2=" + result_2);
}
}

View File

@@ -0,0 +1,19 @@
package ch04;
class Fact {
float fact(int n) { //定义计算n!的方法
int i;
float x = 1;
for (i = 1; i < n; i++)
x = x * n;
return x;
}
}
public class CheckFact {
public static void main(String[] args) {
Fact x = new Fact();
System.out.println(x.fact(10)); //计算10
System.out.println(x.fact(15)); //计算15
}
}

View File

@@ -0,0 +1,33 @@
package ch04;
import java.applet.*;
import java.awt.*;
class Area {
float fun(float r) {
return 3.14159f * r * r;
}
float get(float x, float y) {
return x + y;
}
}
class Circle extends Area {
float fun(float r) {
return 3.14159f * 2.0f * r;
}
}
public class CircleArea extends Applet {
Circle yuan;
public void init() {
yuan = new Circle();
}
public void paint(Graphics g) {
g.drawString("调用子类重写的方法fun(): 园的面积= " + yuan.fun(5.0f), 5, 20);
g.drawString("调用继承父类的方法get(): x+y=" + yuan.get(12.0f, 8.0f), 5, 40);
}
}

View File

@@ -0,0 +1,18 @@
package ch04;
import java.io.*;
public class Crctngle {
public static void main(String[] args) {
Rectangle x1 = new Rectangle(); //声明类的对象并实例化
Rectangle x2 = new Rectangle(50, 40); //声明类的对象并初始化
Rectangle x3 = new Rectangle(x1);
System.out.println("x1.length()=" + x1.length());
System.out.println("x1.width()=" + x1.width());
System.out.println("x2.length()=" + x2.length());
System.out.println("x2.width()=" + x2.width());
System.out.println("x3.length()=" + x3.length());
System.out.println("x3.width()=" + x3.width());
}
}

View File

@@ -0,0 +1,19 @@
package ch04;
class ABC //friendly类
{
public int pub_i = 5;
public void show() {
System.out.println("pub_i=" + pub_i);
}
}
class Demo {
public static void main(String[] args) {
ABC abc = new ABC(); //使用本包中的friendly类
System.out.println("abc.pub_i=" + abc.pub_i);
abc.pub_i = 10;
abc.show();
}
}

View File

@@ -0,0 +1,20 @@
package ch04;
class A {
void callme() {
System.out.println("Inside A's callme( ) method");
}
}
class B extends A {
void callme() {
System.out.println("Inside B's callme( ) method");
}
}
public class Dispatch {
public static void main(String args[]) {
A a = new B();
a.callme();
}
}

View File

@@ -0,0 +1,31 @@
package ch04;
class 动物 {
void cry() {
}
}
class extends 动物 {
void cry() {
System.out.println("狗cry():汪汪.....");
}
}
class extends 动物 {
void cry() {
System.out.println("猫cry(): 喵喵.....");
}
}
class DynamicState {
public static void main(String[] args) {
动物 dongwu;
if (Math.random() >= 0.5) {
dongwu = new (); //上转的对象
dongwu.cry();
} else {
dongwu = new (); //上转的对象
dongwu.cry();
}
}
}

View File

@@ -0,0 +1,11 @@
package ch04;
class Father {
void speak() { //无参数的speak()方法
System.out.println("I am Father! ");
}
void speak(String s) { //有参数的speak(String s)方法
System.out.println("I like" + " " + s + ". ");
}
}

View File

@@ -0,0 +1,37 @@
package ch04;
class superClass {
int x;
superClass() {
x = 3;
System.out.println("in superClass : x = " + x);
}
void doSomething() {
System.out.println("in superClass.doSomething( )");
}
}
class subClass extends superClass {
int x;
subClass() {
super(); //call constructor of superClass
x = 5;
System.out.println("in subClass : x = " + x);
}
void doSomething() {
super.doSomething(); //call method of superClass
System.out.println("in subClass.doSomething( )");
System.out.println("super.x = " + super.x + " sub.x = " + x);
}
}
public class Inheritance {
public static void main(String[] args) {
subClass subC = new subClass();
subC.doSomething();
}
}

View File

@@ -0,0 +1,21 @@
package ch04;
class Fibi {
public long fibinacii(int n) {
long c = 0;
if (n == 1 || n == 2)
c = 1;
else
c = fibinacii(n - 1) + fibinacii(n - 2);
return c;
}
}
public class ItemsFibi {
public static void main(String[] args) {
Fibi a = new Fibi();
for (int i = 1; i <= 10; i++) {
System.out.print(" " + a.fibinacii(i));
}
}
}

View File

@@ -0,0 +1,24 @@
package ch04;
import java.io.*;
class MethodOverloading {
void receive(int i) {
System.out.println("Receive one int data");
System.out.println("i=" + i);
}
void receive(int x, int y) {
System.out.println("Receive two int datas");
System.out.println("x=" + x + " y=" + y);
}
}
public class MethodOverloadingTest {
public static void main(String[] args) {
MethodOverloading mo = new MethodOverloading();
mo.receive(1);
mo.receive(2, 3);
}
}

View File

@@ -0,0 +1,9 @@
package ch04;
public class OverLoadingDemo {
public static void main(String[] args) {
Father x = new Father();
x.speak(); //调用无参的speak()方法
x.speak("music"); //调用有参的speak("music")方法
}
}

View File

@@ -0,0 +1,38 @@
package ch04;
import ch04.tom.langshan.NewDate;
public class People {
private String name;
private NewDate birth;
public static void main(String[] args) {
People a = new People("XiaoYu", 1989, 10, 06);
a.output();
}
public People(String n1, NewDate d1) {
name = n1;
birth = d1;
}
public People(String n1, int y, int m, int d) {
this(n1, new NewDate(y, m, d));
}
public People() {
this("", new NewDate());
}
public int age() { //计算年龄
return NewDate.thisyear() - birth.year(); //获得年份
}
public void output() {
System.out.println("name : " + name);
System.out.println("birth: " + birth.toString());
System.out.println("age : " + age());
}
}

View File

@@ -0,0 +1,31 @@
package ch04;
import java.util.Scanner;
public class PrimeNumber {
/*用(int) Math.sqrt(n)求出循环上限
* isPrime()方法用来检测当前数是否为质数
*/
public static boolean isPrime(int num) {
boolean prime = true;
int limit = (int) Math.sqrt(num);
for (int i = 2; i <= limit; i++) {
if (num % i == 0) {
prime = false;
break;
}
}
return prime;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入您要判断的数:");
int n = input.nextInt();
if (isPrime(n)) {
System.out.println(n + "是质数!");
} else {
System.out.println(n + "不是质数!");
}
}
}

View File

@@ -0,0 +1,20 @@
package ch04;
public class PrvtVar {
private int money;
PrvtVar() {
money = 2000;
}
private int getMoney() {
return money;
}
public static void main(String[] args) {
PrvtVar exa = new PrvtVar();
exa.money = 3000;
int m = exa.getMoney(); //可访问本类成员的private变量
System.out.println("money=" + m);
}
}

View File

@@ -0,0 +1,29 @@
package ch04;
class Rectangle { //矩形类
private int width; //矩形的宽度
private int length; //矩形的长度
Rectangle() { //矩形的不带参数的构造函数缺省的给出长30和宽20
length = 30;
width = 20;
}
Rectangle(int l, int w) { //矩形的带参数的构造函数
length = l;
width = w;
}
Rectangle(Rectangle r) { //此构造方法以另一个Rectangle作为参数
width = r.width(); //通过对象调用函数并附值给相应变量
length = r.length();
}
int width() { //返回宽度
return width;
}
int length() { //返回长度
return length;
}
}

View File

@@ -0,0 +1,18 @@
package ch04;
import java.applet.Applet;
import java.awt.*;
public class RedButton extends Applet {
Button redbutton;
public void init() {
redbutton = new Button("我是一个红色的按钮");
redbutton.setBackground(Color.red);
add(redbutton);
}
public void paint(Graphics g) {
g.drawString("it is a button", 30, 50);
}
}

View File

@@ -0,0 +1,40 @@
package ch04;
class Student {
int number, tel = 81238888;
String name;
Student(int number, String name) {
this.number = number;
this.name = name;
System.out.println("Father构造:I am " + name + "my number is " + number);
}
void show() {
System.out.println("father's show(tel): tel of Student is " + tel);
}
}
class Univer_Student extends Student {
boolean 婚否;
int tel = 81236666;
Univer_Student(int number, String name, boolean b) {
super(number, name); // 使用父类的构造方法
婚否 = b;
System.out.println("Son构造新增属性:婚否=" + 婚否);
}
void showtel() {
System.out.println("Son's showtel(tel):tel of Univer_Student is " + tel);
System.out.println("Son's showtel(super.tel):tel of super is " + super.tel);
}
}
public class ShowStudent {
public static void main(String[] args) {
Univer_Student zhang = new Univer_Student(8030410, " XiaoBin", false);
zhang.showtel();
zhang.show();
}
}

View File

@@ -0,0 +1,38 @@
package ch04;
import java.applet.*;
import java.awt.*;
class Father1 {
private int money;
float weight, height;
String head;
String speak(String s) {
return s;
}
}
class Son extends Father1 {
String hand, foot;
}
public class SubBoy extends Applet {
Son boy;
public void init() {
boy = new Son();
boy.weight = 120f;
boy.height = 1.75f;
boy.head = "一个聪明的大脑袋,";
boy.hand = "两只巧手,";
boy.foot = "一双喜欢瞎跑的脚。";
}
public void paint(Graphics g) {
g.drawString(boy.speak("我是儿子"), 5, 20);
g.drawString(boy.head + boy.hand + boy.foot, 5, 40);
g.drawString("体重:" + boy.weight + " 身高:" + boy.height, 5, 60);
}
}

View File

@@ -0,0 +1,58 @@
package ch04;
class {
double 半径;
(double r) {
半径 = r;
}
double 计算面积() {
return 3.14 * 半径 * 半径;
}
void 修改半径(double 新半径) {
半径 = 新半径;
}
double 获取半径() {
return 半径;
}
}
class 圆锥 {
底圆;
double ;
圆锥( circle, double h) {
this.底圆 = circle;
this. = h;
}
double 计算体积() {
double volume;
volume = 底圆.计算面积() * / 3.0;
return volume;
}
void 修改底圆半径(double r) {
底圆.修改半径(r);
}
double 获取底圆半径() {
return 底圆.获取半径();
}
}
public class Taper {
public static void main(String args[]) {
circle = new (10);
圆锥 circular = new 圆锥(circle, 20);
System.out.println("圆锥底圆半径:" + circular.获取底圆半径());
System.out.println("圆锥的体积:" + circular.计算体积());
circular.修改底圆半径(100);
System.out.println("圆锥底圆半径:" + circular.获取底圆半径());
System.out.println("圆锥的体积:" + circular.计算体积());
}
}

View File

@@ -0,0 +1,20 @@
package ch04;
public class TempConverter {
public static void main(String[] args) {
TempConverter t = new TempConverter();
t.data();
}
protected void data() {
for (int i = -40; i <= 120; i += 10) {
float c = (i - 32) * (5f / 9);
print(i, c);
}
}
protected void print(float f, float c) {
System.out.println("华氏温度" + f + "=摄氏温度" + c);
}
}

View File

@@ -0,0 +1,29 @@
package ch04;
import java.applet.*;
import java.awt.*;
class Chengji {
float fun(float x, float y) {
return x * y;
}
}
class AddChengji extends Chengji {
float fun(float x, float y) {
return x + y;
}
}
public class TestAddChengji extends Applet {
AddChengji sum;
public void init() {
sum = new AddChengji();
}
public void paint(Graphics g) {
g.drawString("sum=" + sum.fun(4, 6), 100, 40);
}
}

View File

@@ -0,0 +1,23 @@
package ch04;
class Point {
int x, y;
Point() {
x = 0;
y = 0;
}
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class TestPoint {
public static void main(String[] args) {
Point p1 = new Point(34, 56);
Point p2 = new Point(21, 67);
p1 = p2;
}
}

View File

@@ -0,0 +1,28 @@
package ch04;
class Text1 {
static int a; //当被定义为static类型时为类变量可被对象或类调用
int b; //对象变量,只能被对象调用
public void display(int a, int b) { //成员方法
System.out.println("static int a=" + a);
System.out.println(" int b=" + b);
}
public static void display(int b) {
System.out.println("static display: int b=" + b);
}
public static void main(String args[]) {
Text1 tt = new Text1();
tt.display(5, 6);
//Text1.display(5,6)是错误的,对象变量或对象方法只能被对象tt调用
Text1.display(0);
//当被定义为static类型时为类方法可被对象或类调用
tt.display(23);
tt.a = 9;
Text1.a = 24;
tt.b = 3;
tt.display(a, 15);
}
}

View File

@@ -0,0 +1,22 @@
package ch04;
import java.applet.*;
import java.awt.*;
class Tongji {
protected float fun(float x, float y) {
return x * y;
}
}
class Addxy extends Tongji {
// float fun(float x,float y) { //非法,因为降低了访问级别
// return x+y ;
// }
}
class Subxy extends Tongji {
public float fun(float x, float y) { //合法,没有降低访问级别.
return x - y;
}
}

View File

@@ -0,0 +1,19 @@
package ch04.sun.com;
import ch04.tom.langshan.Father;
public class Jerry extends Father { //Jerry和Father在不同的包中.
public Jerry() {
super(20);
}
public static void main(String[] args) {
Jerry jerry = new Jerry();
// jerry.height=12; //非法,因为Jerry没有继承友好的height.
jerry.weight = 200; //合法.
jerry.money = 800; //合法.
int m = jerry.getMoney(); //合法..
// jerry.setMoney(300); //非法,因为Jerry没有继承友好的方法setMoney.
System.out.println("m=" + m);
}
}

View File

@@ -0,0 +1,19 @@
package ch04.tom.langshan;
public class Father {
int height;
protected int money;
public int weight;
public Father(int m) {
money = m;
}
protected int getMoney() {
return money;
}
void setMoney(int newMoney) {
money = newMoney;
}
}

View File

@@ -0,0 +1,29 @@
package ch04.tom.langshan;
import java.util.*; //引用java.util包
public class NewDate {
private int year, month, day;
public NewDate(int y, int m, int d) {
year = y;
month = (((m >= 1) & (m <= 12)) ? m : 1);
day = (((d >= 1) & (d <= 31)) ? d : 1);
}
public NewDate() {
this(0, 0, 0);
}
public static int thisyear() { //获得当年的年份
return Calendar.getInstance().get(Calendar.YEAR);
}
public int year() { //获得年份
return year;
}
public String toString() { //转化为字符串
return year + "-" + month + "-" + day;
}
}