2022年09月26日Java上机

运行不了需要建立包,多个文件都包含A类,不建立包会冲突

运行不了需要建立包,多个文件都包含A类,不建立包会冲突

运行不了需要建立包,多个文件都包含A类,不建立包会冲突

示例4-1

JBT4101

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Animal {
public float weight;

public void eat() {
System.out.println("eat......");
}
}

class Mammal extends Animal {
public int heartRate;

public void breath() {
System.out.println("breath......");
}
}

class Cat extends Mammal {
boolean longHair;

public void purr() {
System.out.println("purr......");
}
}

public class JBT4101 {
public static void main(String args[]) {
Cat obj = new Cat();
obj.eat();
obj.breath();
obj.purr();
}
}

示例4-9

JBT4106.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class A {
protected double y=2.13;
void set1(int y){
this.y=y;
}
}
class B extends A {
int y=0;
void show( ){
y=y+10;
System.out.println("y="+y);
}
void set2(int y){
this.y=y;
}
}
class JBT4106 {
public static void main(String args[ ]){
B b=new B();
b.set1(-20);
b.show();
b.set2(-20);
b.show( );
}
}

示例4-13

JBT4110

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class A {
int x, y;

void add(int x, int y) { //note1
System.out.println(x + "+" + y + "=" + (x + y + 100));
}

void add(float x, float y) { //note2
System.out.println(x + "+" + y + "=" + (x + y + 200));
}
}

class B extends A {
void add(int x, long y) { //note3
System.out.println(x + "+" + y + "=" + (x + y + 300));
}

void add(long x, int y) { //note4
System.out.println(x + "+" + y + "=" + (x + y + 400));
}

void add(int x, int y) { //note5
System.out.println(x + "+" + y + "=" + (x + y + 500));
}


}

public class JBT4110 {
public static void main(String args[]) {
B b = new B();
b.add(1, 2); //call 1
b.add(1.0f, 2.0f); //call 2
b.add(1, 2L); //call 3
b.add(1L, 2); //call 4
}
}

示例4-19

JBT4201

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
abstract class Shape {
public int x, y;
public int width, height;

public Shape(int x1, int y1, int width1, int height1) {
x = x1;
y = y1;
width = width1;
height = height1;
}

abstract double getArea(); //抽象方法,只能声明

abstract double getPerimeter(); //抽象方法,只能声明
}

class Square extends Shape {
public double getArea() {
return (width * height);
}

public double getPerimeter() {
return (2 * width + 2 * height);
}

Square(int x, int y, int width, int height) {
super(x, y, width, height);
}
}

class Circle extends Shape {
public double r;

public double getArea() {
return (r * r * Math.PI);
}

public double getPerimeter() {
return (2 * Math.PI * r);
}

Circle(int x, int y, int width, int height) {
super(x, y, width, height);
r = (double) width / 2.0;
}
}

public class JBT4201 {
public static void main(String args[]) {
Square box = new Square(5, 15, 20, 20);
Circle oval = new Circle(5, 50, 20, 20);
System.out.println("Box Area==" + box.getArea());
System.out.println("Oval Area==" + oval.getArea());
}
}

示例4-21

JBT4202B

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
interface A {
char cha = 'A';

public void showA();
}

interface B {
char chb = 'B';

public void showB();
}

interface C extends A, B {
char chc = 'C';

public void showC();
}

interface D {
char chd = 'D';
}

class TestInterface implements C {
public void showA() {
System.out.println(cha);
}

public void showB() {
System.out.println(chb);
}

public void showC() {
System.out.println(chc);
}
}

public class JBT4202B {
public static void main(String[] args) {
TestInterface obj = new TestInterface();
System.out.println(D.chd);
obj.showA();
obj.showB();
obj.showC();
}
}

示例4-22

JBT4203

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
interface ShowMessage {
void show(String s); //接口中的方法默认都是public
}

class Car implements ShowMessage {
public void show(String s) { //在使用接口的类中要显式表示public
System.out.println("Car=" + s);
}
}

class Bicycle implements ShowMessage {
public void show(String s) { //在使用接口的类中要显式表示public
System.out.println("Bicycle=" + s);
}
}

public class JBT4203 {
public static void main(String args[]) {
ShowMessage dm; //声明接口变量
dm = new Car(); //接口变量中存放对象的引用
dm.show("奔驰"); //接口回调
dm = new Bicycle(); //接口变量中存放对象的引用
dm.show("捷安特"); //接口回调
}
}

2022年09月26日Java上机
https://www.eldpepar.com/coding/45619/
作者
EldPepar
发布于
2022年9月26日
许可协议