Skip to content

Commit e2c2ca4

Browse files
committed
test
1 parent 01d50c2 commit e2c2ca4

File tree

4 files changed

+226
-0
lines changed

4 files changed

+226
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.practise;
2+
3+
/*
4+
*编写类Animal,该类包含私有属性name、age,公有方法void showInfo()。为Animal类添加构造器和属性访问器。
5+
* 定义主类Demo9,在主方法中实例化Animal类,并且用构造器和属性访问器分别为name和age赋值,调用showInfo方法输出属性信息。
6+
* */
7+
public class practise3_Animal {
8+
public static void main(String[] args) {
9+
Animal a = new Animal("cat",10);
10+
// a.setAge(5);
11+
a.showInfo();
12+
}
13+
}
14+
/*一个文件只能有一个public,Animal必须为内部类*/
15+
class Animal{
16+
private String name;
17+
private int age;
18+
public Animal(String name,int age){
19+
this.name = name;
20+
this.age = age;
21+
}
22+
//属性访问器 start
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public int getAge() {
32+
return age;
33+
}
34+
35+
public void setAge(int age) {
36+
this.age = age;
37+
}
38+
//属性访问器 end
39+
public void showInfo(){
40+
System.out.println("名字是:"+name);
41+
System.out.println("年龄是:"+age+"岁");
42+
}
43+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.practise;
2+
3+
/*
4+
* 编写类Person,该类包含公有属性name、age,公有方法void showInfo(),方法输出两个属性name和age。
5+
* 编写Person类的子类Student类,该类中定义公有属性StuID,重写父类的showInfo方法,输出三个属性的信息。
6+
* 定义主类,在主方法中实例化Student类和Person类,为每个属性赋值,调用showInfo()方法输出所有属性信息。
7+
* */
8+
public class practise4_Person {
9+
public static void main(String[] args) {
10+
Person person = new Person("ash",23);
11+
Student student = new Student("taom",21,"efetr345");
12+
person.showInfo();
13+
student.showInfo();
14+
}
15+
}
16+
17+
class Person {
18+
public String name;
19+
public int age;
20+
//***必须有空构造函数,否则 Student 的构造函数或报错
21+
public Person(){
22+
}
23+
public Person (String name,int age){
24+
this.name = name;
25+
this.age = age;
26+
}
27+
public void showInfo() {
28+
System.out.println("名字是:"+name);
29+
System.out.println("年龄是:"+age+"岁");
30+
}
31+
}
32+
class Student extends Person{
33+
public String StuID;
34+
public Student(String name, int age, String StuID) {
35+
this.name=name;
36+
this.age=age;
37+
this.StuID=StuID;
38+
}
39+
public void showInfo() {
40+
System.out.println("名字是:"+name);
41+
System.out.println("年龄是:"+age+"岁");
42+
System.out.println("ID是:"+StuID);
43+
}
44+
}
45+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.practise;
2+
3+
/*
4+
* 编写接口Shape,该接口包含符号常量PI,抽象方法getArea()。定义两个普通类Rectangle和Circle,两个类都要求声明Shape接口。
5+
* 在Rectangle类中复写getArea()方法求矩形面积,在Circle类中复写getArea()方法求圆形面积。在主函数中分别定义两个类的实例,
6+
* 矩形边长输入4和6,圆形输入半径值为3,求各自的面积并输出。
7+
* */
8+
public class practise5_Rectangle {
9+
public static void main(String[] args) {
10+
int a = 4;
11+
int b = 6;
12+
int r = 3;
13+
Rectangle rec = new Rectangle(a,b);
14+
Circle cir = new Circle(r);
15+
System.out.println("矩形的面积是"+rec.getArea());
16+
System.out.println("圆形的面积是"+cir.getArea());
17+
}
18+
}
19+
interface Shape {
20+
public static final double PI=3.14;
21+
public abstract double getArea();
22+
}
23+
24+
class Rectangle implements Shape{
25+
private double a;
26+
private double b;
27+
public Rectangle(double a, double b) {
28+
this.a=a;
29+
this.b=b;
30+
}
31+
public double getArea() {
32+
return a*b;
33+
}
34+
}
35+
class Circle implements Shape{
36+
double r;
37+
public Circle(double r) {
38+
this.r=r;
39+
}
40+
public double getArea() {
41+
return PI*r*r;
42+
}
43+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
2+
3+
#### 1.1. 接口概念
4+
5+
> 在 Java 程序设计语言中, 接口不是类,而是对类的一组需求描述,这些类要遵从接口描述的统一格式进行定义。
6+
>
7+
> 实际上, 接口可以提供多重继承的大多数好处,同时还能避免多重继承的复杂性和低效性
8+
>
9+
> ```java
10+
> 我们经常听到服务提供商这样说:“ 如果类遵从某个特定接口,那么就履行这项服务。”
11+
> 例如,Arrays 类中的sort 方法承诺可以对对象数组进行排序,但要求满足下列前提:对象所属的类必须实现了 Comparable接口。
12+
> public interface Comparable
13+
> {
14+
> int compareTo(Object other);
15+
> }
16+
>
17+
> /*
18+
> 这就是说, 任何实现 Comparable 接口的类都需要包含 compareTo 方法,并且这个方法的参
19+
> 数必须是一个 Object 对象, 返回一个整型数值。
20+
> */
21+
> ```
22+
>
23+
> 接口中的所有方法自动地属于 public。 因此, 在接口中声明方法时, 不必提供关键字
24+
> public
25+
>
26+
> 提供实例域和方法实现的任务应该由实现接口的那个类来完成。因此, 可以将接口看成是没有实例域的抽象类 。
27+
>
28+
> ```java
29+
> 假设希望使用 Arrays 类的 sort 方法对 Employee 对象数组进行排序, Employee
30+
> 就必须实现 Comparable 接口。
31+
> 为了让类实现一个接口, 通常需要下面两个步骤:
32+
> 1.将类声明为实现给定的接口
33+
> 2.对接口中的所有方法进行定义
34+
> 要将类声明为实现某个接口, 需要使用关键字 implements:
35+
> class Employee impleients Comparable
36+
>
37+
> 当然, 这里的 Employee 类需要提供 compareTo 方法。 假设希望根据雇员的薪水进行比较.
38+
> 以下是 compareTo 方法的实现:
39+
> public int compareTo(Object otherObject)
40+
> {
41+
> Employee other = (Employee) otherObject ;
42+
> return Double,compare(salary, other ,salary);
43+
> }
44+
> ```
45+
46+
47+
48+
#### 1.2.接口的特性
49+
50+
> 1.然而, 尽管不能构造接口的对象,却能声明接口的变量:
51+
>
52+
> ```java
53+
> Comparable x; // OK
54+
> ```
55+
>
56+
> 接口变量必须引用实现了接口的类对象:
57+
>
58+
> ```java
59+
> x = new Employee(. . .); // OK provided Employee implements Comparable
60+
> ```
61+
>
62+
> 2.接下来, 如同使用 instanceof检查一个对象是否属于某个特定类一样, 也可以使用
63+
> instance 检查一个对象是否实现了某个特定的接口:
64+
>
65+
> ```java
66+
> if (anObject instanceof Comparable) { . . . }
67+
> ```
68+
>
69+
> 3.与可以建立类的继承关系一样,接口也可以被扩展。这里允许存在多条从具有较高通用
70+
> 性的接口到较高专用性的接口的链。例如, 假设有一个称为 Moveable 的接口:
71+
>
72+
> ```java
73+
> public interface Moveable
74+
> {
75+
> void move(double x, double y);
76+
> }
77+
> 然后, 可以以它为基础扩展一个叫做 Powered 的接口:
78+
> public interface Powered extends Moveable
79+
> {
80+
> double milesPerCallon();
81+
> }
82+
> ```
83+
>
84+
> 4. 虽然在接口中不能包含实例域或静态方法,但却可以包含常量。例如
85+
>
86+
> ```java
87+
> public interface Powered extends Moveable
88+
> {
89+
> double milesPerCallon();
90+
> double SPEED_LIHIT = 95; // a public static final constant
91+
> }
92+
> //与接口中的方法都自动地被设置为 public —样, 接口中的域将被自动设为 public static final 。
93+
> ```
94+
>
95+
>

0 commit comments

Comments
 (0)