java面向对象小结上

java 小结,内容面向对象上

关于重载

重载:如果同一个类中包含了两个或两个以上方法的方法名相同,但是形参列表不同,则称方法为重载。
比如计算圆和三角形的面积,都同时用area方法计算,这个时候就可以重载。

以下为代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package hello;


public class The_area {
public void Area(double a,double b,double c) {
double p = (a+b+c)/2;
double ans = Math.sqrt(p*(p-a)*(p-b)*(p-c));
System.out.println("the area is " + ans);
}
public void Area(double r) {
double ans = Math.PI*r*r;
System.out.println("the area is " + ans);
}
public static void main(String[] args) {
// TODO Auto-generated method stub

The_area t = new The_area();
t.Area(3,4,5);
The_area c = new The_area();
c.Area(3);
}

}

关于隐藏和封装

访问控制符,拉的第一个可能略有点丑。。。

private default protected public
同一个类中 V V V V
同一个包中 V V V
子类中 V V
全局范围内 V
这个是控制符的访问级别

关于封装:指的是将对象的状态信息隐藏在对象内部,不允许外部程序直接访问对象内部信息,而是通过方法来实现对内部信息的操作和访问。

以下是一个关于封装的代码

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
package hello;

class Person{
private String name;
private int age;
public void setName(String name) {
if(name.length()>6 || name.length()<2) {
System.out.println("您设置的人名不符合要求");
return;
}
else {
this.name = name;
}
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age>100 || age<0) {
System.out.println("您设置的年龄不合法");
return ;
}
else
this.age = age;
}

}


public class PersonTest {

public static void main(String[] args) {
// TODO Auto-generated method stub
var p = new Person();
// p.age = 10;该语句错误,因为类的成员变量被隐藏了,不能这么访问
p.setAge(1000);
System.out.println("the age is " + p.getAge());
p.setAge(30);
p.setName("weige");
System.out.println("name: " + p.getName() + "age" + p.getAge());
}

}

关于构造器

一般使用构造器来在创建对象时执行初始化,比如,以下,这样可以更好的实现封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package hello;

class Me{
public String name;
public int age;
public int high;
public Me(String name,int age,int high) {
this.name = name;
this.age = age;
this.high = high;
}
}

public class About_me {

public static void main(String[] args) {
// TODO Auto-generated method stub
Me w = new Me("weige",19,180);
System.out.printf("%s %d %d",w.name,w.age,w.high);
}

}

关于继承

java的继承具有单继承的特点,即每个子类只有继承一个父类

以下是继承的实例,

示例1:

代码:

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
package hello;


class Animal{
private void beat() {
System.out.println("心脏跳动……");
}
public void breathe() {
beat();
System.out.println("吸一口气,吐一口气,呼吸中");
}
}

class BBird extends Animal{
public void fly() {
System.out.println("我在天空自在的飞翔");
}
}

class Wolf extends Animal{
public void run() {
System.out.println("我在陆地上快速的奔跑");
}
}

public class InheritTest {

public static void main(String[] args) {
// TODO Auto-generated method stub
BBird b = new BBird();
b.breathe();
b.fly();
Wolf w = new Wolf();
w.breathe();
w.run();
}

}

子类继承父类示例2:
可以调用父类的构造器,来初始化

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
56
57
58
package win_java;


class Vehicle {
double speed;
String kind;
String color;
public Vehicle(double s,String k,String c) {
speed = s;
kind = k;
color = c;
}

public void setKind(String kind) {
this.kind = kind;
}

public void setColor(String color) {
this.color = color;
}

public void setSpeed(double speed) {
this.speed = speed;
}
public double getSpeed() {
return speed;
}
public String getKind() {
return kind;
}
public String getColor() {
return color;
}

}



class Car extends Vehicle{
int passenger;

Car(double s,String k,String c,int p) {
super(s,k,c);
passenger = p;
}

public void get_seats() {
System.out.println("the car owns " + passenger + "seats");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Car mycar = new Car(300,"卡车","red",5);
mycar.setColor("black");
mycar.setSpeed(350);
System.out.println(mycar.getColor());
}

}

调用父类构造器来初始化

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
package hello;

class Base{
public double size;
public String name;
public Base(double size,String name)
{
this.size = size;
this.name = name;
}
public double getSize() {
return size;
}
public String getName() {
return name;
}

}



public class Sub extends Base{

public String color;
public Sub(double size,String name,String color)
{
super(size,name);
this.color = color;
}


public String getColor() {
return color;
}


public static void main(String[] args) {
// TODO Auto-generated method stub
var s = new Sub(5.6,"调试对象","红色");
System.out.println(s.getSize() + s.getName() + s.getColor());


}

}

多态

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package hello;

class BaseClass{
public int book = 6;
public void base() {
System.out.println("父类的普通方法");
}
public void test() {
System.out.println("父类被覆盖的方法");
}
}

public class SubClass extends BaseClass{

public String book ="java入门";
public void test() {
System.out.println("子类的覆盖父类的方法");
}
public void sub() {
System.out.println("子类的普通方法");
}

public static void main(String[] args) {
// TODO Auto-generated method stub
BaseClass bc = new BaseClass();
System.out.println(bc.book);
bc.base();
bc.test();
// 编译时类型和运行时类型完全一样所以不存在多态
SubClass sc = new SubClass();
System.out.println(sc.book);
// 输出了java入门
// 从父类继承base方法
sc.base();
// 执行当前类的test方法,此时多态发生,因为同时使用了父类,也运用了子类
sc.test();
// 编译时和类型不一样,多态发生
BaseClass pBc = new SubClass();
System.out.println(pBc.book);
// 输出了6
pBc.base();
// 父类的普通方法
pBc.test();
// 输出了子类的覆盖父类的方法
System.out.println();
}

}

组合实现复用两种方式吧

way1

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
package hello;


class Animal{
private void beat() {
System.out.println("心脏跳动……");
}
public void breathe() {
beat();
System.out.println("吸一口气,吐一口气,呼吸中");
}
}

class BBird extends Animal{
public void fly() {
System.out.println("我在天空自在的飞翔");
}
}

class Wolf extends Animal{
public void run() {
System.out.println("我在陆地上快速的奔跑");
}
}

public class InheritTest {

public static void main(String[] args) {
// TODO Auto-generated method stub
BBird b = new BBird();
b.breathe();
b.fly();
Wolf w = new Wolf();
w.breathe();
w.run();
}

}

way2

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
package hello;


class Animal{
private void beat() {
System.out.println("心脏跳动……");
}
public void breathe() {
beat();
System.out.println("吸一口气,吐一口气,呼吸中……");
}
}

class Birdd{
private Animal a;
public Birdd(Animal a) {
this.a = a;
}
public void breathe() {
a.breathe();
}
public void fly() {
System.out.println("我在天空自在的飞翔……");
}
}

class Wolff{
private Animal a;
public Wolff(Animal a) {
this.a = a;
}
public void breathe() {
a.breathe();
}
public void run() {
System.out.println("我在陆地上快速奔跑");
}
}

public class CompositeTest {

public static void main(String[] args) {
// TODO Auto-generated method stub
var a1 = new Animal();
var b = new Birdd(a1);
b.breathe();
b.fly();
var a2 = new Animal();
var w = new Wolff(a2);
w.breathe();
w.run();
}

}

抽象类

抽象方法必须使用abstract修饰符来定义,抽象类可以没抽象方法

注意:
总结就是:抽象类可用有得有失来总结,就是,抽象类可以包含抽象方法,就是抽象类不能用于创建实例。

抽象类示例

示例1
代码:

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
package hello;

abstract class Shape{
double radius,length,width,height;
abstract double vol();
Shape(double r,double h){
radius = r;
height = h;
}
Shape(double l,double w,double h){
length = l;
width = w;
height = h;

}
}

class Circle extends Shape{
Circle(double r,double h){
super(r,h);
}
double vol() {
return 3.1416*radius*radius*height;
}
}

class Rectangle extends Shape{
public Rectangle(double l,double w,double h) {
// TODO Auto-generated constructor stub
super(l,w,h);
}
double vol() {
return length*width*height;
}

}


public class AbstractClassDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub
var c = new Circle(2, 3);
System.out.println(c.vol());
var r = new Rectangle(3, 2, 4);
System.out.println(r.vol());
}

}

示例2
代码:

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
package hello;

abstract class Commonn{
public double price,amount;
abstract double calculate();
Commonn(double p,double a) {
price = p;
amount = a;
}
}

class Summer extends Commonn{
Summer(double price,double amount){
super(price,amount);
}
double calculate() {
return price*amount;
}
}

class Winter extends Commonn{
Winter(double price,double amount){
super(price,amount);
}
double calculate() {
return price*(amount+20);
}
}

public class abst_learning {

public static void main(String[] args) {
// TODO Auto-generated method stub
Summer s = new Summer(0.49,125);
System.out.println("summer " + s.calculate());
Winter w = new Winter(0.49,125);
System.out.println("Winter " + w.calculate());
}

}

基础概念还是得多用,实际操作可能会出现不同的意想不到的结局,比如同一个package内不能存在名字相同的类,比如在同一个class 中不能用public修饰的细节,路漫漫其修远兮。。。bug慢慢理。。。

-------------本文结束感谢您这么好看还看我的文章-------------