第2章 面向对象
2.1 不同修饰符使用细节
常用来修饰类、方法、变量的修饰符如下:
public 权限修饰符,公共访问, 类,方法,成员变量
protected 权限修饰符,受保护访问, 方法,成员变量
默认什么也不写 也是一种权限修饰符,默认访问, 类,方法,成员变量
private 权限修饰符,私有访问, 方法,成员变量
static 静态修饰符 方法,成员变量
final 最终修饰符 类,方法,成员变量,局部变量
abstract 抽象修饰符 类 ,方法
我们编写程序时,权限修饰符一般放于所有修饰符之前,不同的权限修饰符不能同时使用;
同时,abstract与private不能同时使用;abstract类必须重写,而private、static、final存在限制
同时,abstract与static不能同时使用;
同时,abstract与final不能同时使用。
public static void main(String[] args) {
//调用方法operatorPerson,传递Person类型对象
Person p = new Person();
operatorPerson§;
operatorPerson(new Person());
}
修饰类能够使用的修饰符:
修饰类只能使用public、默认的、final、abstract关键字
使用最多的是 public关键字
public class Demo {} //最常用的方式
class Demo2{}
public final class Demo3{}
public abstract class Demo4{}
修饰成员变量能够使用的修饰符:
public : 公共的
protected : 受保护的
: 默认的
private :私有的
final : 最终的
static : 静态的
修饰成员变量使用最多的是 private
public int count = 100;
protected int count2 = 100;
int count3 = 100;
private int count4 = 100; //最常用的方式
public final int count5 = 100;
public static int count6 = 100;
修饰构造方法能够使用的修饰符:
public : 公共的
protected : 受保护的
: 默认的
private :私有的
使用最多的是 public
public Demo(){} //最常用的方式
protected Demo(){}
Demo(){}
private Demo(){}
修饰成员方法能够使用的修饰符:
public : 公共的
protected : 受保护的
: 默认的
private :私有的
final : 最终的
static : 静态的
abstract : 抽象的
使用最多的是 public
public void method1(){}//最常用的方式
protected void method2(){}
void method3(){}
private void method4(){}
public final void method5(){}
public static void method6(){}//最常用的方式
public abstract void method7();//最常用的方式
自定义数据类型的使用
3.1 辨析成员变量与方法参数的设计定义
定义长方形类,包含求周长与求面积的方法
定义数学工具类,包含求两个数和的二倍与求两个数积的方法
思考:这两个类的计算方法均需要两个数参与计算,请问两个数定义在成员位置还是形参位置更好,为什么?
如果变量是该类的一部分时,定义成成员变量。
如果变量不应该是类的一部分,而仅仅是功能当中需要参与计算的数,则定义为形参变量。
数学工具类
public class MathTool {
//求两个数的和的二倍
public double sum2times(int number,int number2) {
return (number+number2)*2;
}
//求两个数的积
public double area(int number,int number2) {
return number*number2;
}
}
长方形类
public class CFX {
//因为长与宽,在现实事物中属于事物的一部分,所以定义成员变量
private int chang;
private int kuan;
public CFX(int chang, int kuan) {
this.chang = chang;
this.kuan = kuan;
}
//求长与宽的周长
public double zhouChang() {
return (chang+kuan)*2;
}
//求长与宽的面积
public double mianJi() {
return chang*kuan;
}
public int getChang() {
return chang;
}
public void setChang(int chang) {
this.chang = chang;
}
public int getKuan() {
return kuan;
}
public void setKuan(int kuan) {
this.kuan = kuan;
}
}
3.2 类作为方法参数与返回值
Person类当做方法的参数
Person类型写在方法的参数列表中
package cn.itcast.classes;
public class Person {
private String name = "张三";
public void eat(){
System.out.println(name+ " 在吃饭");
}
public void run(){
System.out.println(name+" 在跑步");
}
}
package cn.itcast.classes;
/*
* Person类,当作方法的参数
* Person类型写在方法的参数列表中
*/
public class TestArguments {
public static void main(String[] args) {
//调用方法operatorPerson,传递Person类型对象
Person p = new Person();
operatorPerson(p);
//传递有名对象p
operatorPerson(new Person());
//传递匿名对象,只能用一次,个人感觉,匿名对象可以理解为:定义了一个你不知道名字的类x,x用完就释放了
//但是在operatorPerson内存调用过这个名字x,x.eat() x.run()
}
/*
* 方法operatorPerson,参数类型是Person类型
* 调用方法operatorPerson,必须传递Person类型的对象
*/
public static void operatorPerson(Person p){
//可以使用引用类型p调用Person类的功能
p.eat();
p.run();
}
}
output:
张三 在吃饭
张三 在跑步
张三 在吃饭
张三 在跑步
3.3 抽象类作为方法参数与返回值
抽象类作为方法参数
a是抽象类Animal cat是抽象类的子类
传参过程 方法:operatorAnimal(Animal a),用到的是父类抽象类,传递进来的是子类cat c(只有传cat才能有编译,因为Animal抽象没有主体)
其实就是多态:Animal a = new cat:
这种方法的优点:扩展性,既可以传cat又可以传dog条件:这两个都是Animal的子类,具有共同抽象方法eat
operatorAnimal©;
也可以;operatorAnimal( new Dog()); 也可以调用Dog的eat方法
package cn.itcast.abstractclass;
/*
* 将抽象类类型,作为方法的参数进行传递
*/
public class TestArguments {
public static void main(String[] args) {
//调用operatorAnimal,传递子类对象
Cat c = new Cat();
operatorAnimal(c);
operatorAnimal( new Dog());
}
/*
* 方法operatorAnimal,参数是一个抽象类
* 调用方法,传递Animal类型对象,Animal抽象类没有对象
* 只能传递Animal的子类的对象 (多态)
* 可以传递Animal的任意的子类对象
*/
public static void operatorAnimal(Animal a){
//引用变量a,调用方法eat
a.eat();
}
}
** 抽象类作为方法返回值
public class GetAnimal {
/*
* 定义方法,方法的返回值是Animal类型
* 抽象类,抽象类没有对象的,因此在方法的return后,返回Animal 的子类的对象
*/
public Animal getAnimal(int i){ //要分清类的定义和方法的定义,方法的定义必须有返回值
if(i==0)
return new Cat();
return new Dog();
}
}
public class TestReturn {
public static void main(String[] args) {
//调用GetAnimal类的方法,getAnimal,返回值是Animal
GetAnimal g = new GetAnimal();
Animal a= g.getAnimal(9);//方法的返回了Animal类型,return new Cat()
//getAnimal 返回的是Animal类,因此不能用cat类作为接收
//Cat a = g.getAnimal(9); 这是错误的
//问题:Animal不是抽象类吗,它还能eat?
//答案是肯定的,因为返回的类型虽然是Animal,但是return的是他的子类Dog或者Cat
//因此:这种方法可以理解为:Animal a= g.getAnimal(9); 等价于 Animal a = new(Cat)/new(Dog)
//好处在于:可以通过给getAnimal设定参数,选择返回的类型是Cat或者Dog,比之前死的返回这一种要好用
a.eat();
}
}
Python抽象类和抽象方法的定义
from abc import ABCmeta, abstractmethod
class People(metaclass=ABCmeta): #创建抽象类
def __init__(self):
pass
@abstractmethod #定义抽象方法
def introduce_yourself(self):
pass
@abstractmethod
def say(self):
print("I'm like you!!")
class Student(People):
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def say(self):
print("hello world")
def introduce_yourself(self):
print("Name is {}, age is {}, sex is {} ".format(self.name, self.age, self.sex))
stu = Student("nlj", 30, "female")
stu.say()
3.4 接口作为方法参数与返回值
接口作为方法参数
接口作为方法参数的情况是很常见的,经常会碰到。当遇到方法参数为接口类型时,那么该方法要传入一个接口实现类对象。如下代码演示。
//接口
interface Smoke{
public abstract void smoking();
}
//接口定义抽象方法: interface Smoke{
//创建子类实现接口Smoke: class Student implements Smoke{
//创建测试类:public class Test {
//在测试类中创建方法调用子类实现接口 public static void method(Smoke sm){//接口作为参数
//实际是通过sm变量调用smoking方法,这时实际调用的是Student对象中的smoking方法
//问题:如果有两个子类实现了Smoke接口的抽象方法,那么效果如何呢?
//答案:取决于你的Test中定义的子类属于哪一个实现类 STudent ?或者Student1
//Smoke s = new Student();//调用method方法,method(s);
class Student implements Smoke{
@Override
public void smoking() {
System.out.println("别抽烟");
}
}
class Student1 implements Smoke{
@Override
public void smoking() {
System.out.println("老子就抽烟");
}
}
//测试类
public class Test {
public static void main(String[] args) {
//通过多态的方式,创建一个Smoke类型的变量,而这个对象实际是Student
Smoke s = new Student();
//调用method方法
method(s); //等价于 method(new Student()) 利用匿名对象
Smoke a = new Student1();
method(a);
}
//定义一个方法method,用来接收一个Smoke类型对象,在方法中调用Smoke对象的show方法
public static void method(Smoke sm){//接口作为参数
//通过sm变量调用smoking方法,这时实际调用的是Student对象中的smoking方法
sm.smoking();
}
}
输出:
别抽烟
老子就抽烟
3.4 接口作为方法参数与返回值
接口作为方法返回值
接口作为方法返回值的情况,在后面的学习中会碰到。当遇到方法返回值是接口类型时,那么该方法需要返回一个接口实现类对象。如下代码演示。
1、定义接口:interface Smoke{
2、定义接口的实现类:class Student implements Smoke{
3、测试类中创建method方法 返回值类型是Smoke
`Smoke s = method();`
//调用method方法的返回值类型是Smoke,接收方式是左边的方法
4、调用Smoke的方法s.smoking()
问题:怎么确定返回的Smoke接口中的重写方法对应的是Student?
答案:因为在Method中写出了调用的接口的实现类是Student,所以能够对应找到s.smoking()
如果你选择返回的是Student1的重写方法smoking,则需要修改method中的
Smoke sm = new Student();→Smoke sm = new Student1();
//接口
interface Smoke{
public abstract void smoking();
}
class Student implements Smoke{
@Override
public void smoking() {
System.out.println("课下吸口烟,赛过活神仙");
}
}
class Student1 implements Smoke{
@Override
public void smoking() {
System.out.println("哈哈哈哈哈哈哈");
}
}
//测试类
public class Test {
public static void main(String[] args) {
//调用method方法,获取返回的会吸烟的对象
Smoke s = method();
//通过s变量调用smoking方法,这时实际调用的是Student对象中的smoking方法
s.smoking();
}
//定义一个方法method,用来获取一个具备吸烟功能的对象,并在方法中完成吸烟者的创建
public static Smoke method(){
Smoke sm = new Student();
return sm;
}
}
下面的代码解析:
1、定义一个抽象类Animal,抽象类的抽象方法是eat() 一个返回值类型是Animal类静态方法getInstance()(实际上返回的是Cat子类,所以有eat的重写方法)
2、定义一个cat子类继承抽象类Animal
3、下面这个代码,表面上没有cat实际上实在执行cat类中的eat
4、import java.util.Calendar;导入日历类,这个类实际上是一个抽象类
Animal a = Animal.getInstance();
a.eat();
package cn.itcast.demo03;
public abstract class Animal {
public abstract void eat();
/*
* 抽象类Animal,定义方法,返回值是Animal类型
* 抽象类没有对象,此方法方便调用,写为静态修饰
*/
public static Animal getInstance(){
return new Cat();
}
}
package cn.itcast.demo03;
public class Cat extends Animal{
public void eat(){
System.out.println("猫吃鱼");
}
}
package cn.itcast.demo03;
import java.util.Calendar;
public class Test {
public static void main(String[] args) {
//直接调用抽象类的静态方法getInstance获取抽象类的子类的对象
//抽象类的静态方法,返回了自己的子类对象
//对于调用者来讲: 不需要关注子类是谁
Animal a = Animal.getInstance();
a.eat();
//日历类
Calendar c = Calendar.getInstance();
System.out.println(c);
}
}
代码:day14 cn.itcast.hotel
接口是用来定义特殊特征(方法)的
继承抽象类 是用来重写共有特征(方法)的
厨师、服务员、经理的共有的 定义在abstract中
经理有一个private double money; 其他的都是public