> 文章列表 > 12.类的封装,继承,多样性

12.类的封装,继承,多样性

12.类的封装,继承,多样性

四.类的封装

1.代码(两个类,将方法封装在另外一个类中,访问权限)

package 第四章;

class Date3 {//Date3开始的地方

private int year,month,day;

public void setday(int y,int m,int d) {//类中的方法setday(),设置日期

    year=y;

    month=m;

    day=d;

}

void today() {//类中的方法,打印出今天的日期

    System.out.println("The day of today is "+year+"/"+month+"/"+day);

}

int getyear() {//类中的方法,获得年份

    return year;

}

boolean isleap(int y) {//类中的方法,判断是否是闰年

    return(y%4==0&&y%100!=0)||y%400==0;}

}//Date3截止的地方

    public class Date3_ex{//公共类Date3_ex

    public static void main(String args[]) {//主函数入口,函数是从主函数开始入口的

        Date3 de=new Date3();//创建对象,实例化Date3

        int year;

        de.setday(2000, 2, 29);//设置日期

        year=de.getyear();

        if(de.isleap(year));//调用方法

        System.out.println(year+"is a leap year");

        de.today();//调用方法

}

2.代码(静态成员)

package 第四章;

class Melon{//类,用来封装方法

float weight;

static float total_weight=0;

static int total_number=0;

public Melon(float w) {//定义方法,与类名相同,用来赋值

weight=w;//赋值

total_weight=total_weight+weight; //总重=总重+变量

total_number=total_number+1;//总量=总量+1

}

public void reduce(){//方法

total_weight=total_weight-weight;//总重=总重-变量

total_number=total_number-1;//总量=总量-1

}

}

public class Melon_ex{//定义一个公共类

public static void main(String args[]){ //主函数,程序执行入口

    Melon m1,m2;//创建对象声明对象

    m1=new Melon(10);//实例化对象

    //创建对象m1时候用参数10调用了类的构造方法,执行语句后,总重=10,总量=1

    m2=new Melon(12);//实例化对象

    //创建对象m2时候用参数12调用了类的构造方法,执行语句后,总重=22,总量=2

System.out.println("Total weight: "+Melon.total_weight);//输出总重,total_weight=22

System.out.println("Total number: "+Melon.total_number);//输出总数量

//由于类变量是静态变量,第一次调用构造方法时总重和总量运算结果被保留六,第二次调用构造方法将以第一次调用方法的运算结果作为初值

m1.reduce();//调用方法1,变化了为10

System.out.println("Total weight:"+Melon.total_weight);//

System.out.println("Total number: "+Melon.total_number);

m2.reduce();//调用方法

System.out.println("Total weight: "+Melon.total_weight);

System.out.println("Total number: "+Melon.total_number);

}

}

3.代码(改变类变量的的访问权限为private,需要构造方法来获取类变量)

package 第四章;

class Melon{//类,用来封装方法

float weight;

private static float total_weight=0;

private static int total_number=0;

public Melon(float w) {//定义方法,与类名相同,用来赋值

weight=w;//赋值

total_weight=total_weight+weight; //总重=总重+变量

total_number=total_number+1;//总量=总量+1

}

public void reduce(){//方法

total_weight=total_weight-weight;//总重=总重-变量

total_number=total_number-1;//总量=总量-1

}

public static float get_total_weight(){//方法得到总重的值

    return total_weight;

}

public static int get_total_number(){//方法得到总量的值

    return total_number;

}

}

public class Melon_ex{//定义一个公共类

public static void main(String args[]){ //主函数,程序执行入口

    Melon m1,m2;//创建对象声明对象

    float t_weight; //定义

    int t_number;//定义

    m1=new Melon(10);//实例化对象

    //创建对象m1时候用参数10调用了类的构造方法,执行语句后,总重=10,总量=1

    m2=new Melon(12);//实例化对象

    //创建对象m2时候用参数12调用了类的构造方法,执行语句后,总重=22,总量=2

    t_weight=Melon.get_total_weight();

    t_number=Melon.get_total_number();

System.out.println("Total weight: "+t_weight);//输出总重,total_weight=22

System.out.println("Total number: "+t_number);//输出总数量

//由于类变量是静态变量,第一次调用构造方法时总重和总量运算结果被保留六,第二次调用构造方法将以第一次调用方法的运算结果作为初值

m1.reduce();//调用方法1,变化了为10

t_weight=Melon.get_total_weight();

t_number=Melon.get_total_number();

System.out.println("Total weight:"+t_weight);//

System.out.println("Total number: "+t_number);

}

}

4,代码(数学方法)

package 第四章;

public class 求正弦值 {

    public static void main(String args[]){

         double x=30;

        System.out.println("x="+x);

        System.out.println("sin(x)="+Math.sin(x*3.14/180));

    }

}

5、知识点

【1】封装的目的

【2】访问权限的设置

【3】类成员(静态成员)

五.类的继承

1.代码(继承子类)

package 第四章;

class Person {//定义类

    protected String name;

    protected int age;

    protected String address;

    public void setPerson(String na,int ag,String ad){ //定义方法,姓名,年龄,地址

    name=na;age=ag;address=ad;

    }

}

class Student extends Person{//定义类

    String address; //与超类中定义的变量一致

    String department;//定义新增变量

    public void setStudent(String na,int ag,String ad,String ad1,String de){//定义方法,继承了超类中的非私有变量

    setPerson(na,ag,ad);

    address=ad1;

    department=de;

    }

    }

    public class Student_ex{//主函数

    public static void main(String args[]){//主函数,程序入口

    Person pe=new Person(); //创建对象

    Student st=new Student();//创建对象

    pe.setPerson("Tom",20,"121 North street");//实例化后对对象进行赋值

    st.setStudent("John",19,"234 South street","336 West Street","Computer");//实例化后对对象进行赋值

    System.out.println("The object of Person:"+pe.name+","+pe.age+","+pe.address);//1的方法输出

    System.out.println("The object of Student:"+st.name+","+st.age+","+st.address+""+st.department);

    }

    }

2.代码(增加了构造方法)

package 第四章;

class Person1 {//类,父类,超类

    protected String name;

    protected int age;

    public Person1(String na,int ag) {//定义方法

    name=na;age=ag;

    }

    public void print(){//定义方法,打印

    System.out.println("The obiect ofPerson:"+name+","+age);

    }

}

    class Student1 extends Person1{//类,子类

    String address;

    String department;

    public Student1(String na, int ag,String ad,String de){ //定义方法

        super(na,ag);//调用父类的变量

        address=ad;

        department=de;

    }

    public void print(){//定义方法打印

        System.out.println("The object of Student:"

    +name+","+age+","+address+","+department);

    }

    }

    public class Student1_ex{//公共类

    public static void main(String args[]){//主函数,程序入口处

    Person1 pe=new Person1("Tom",20);//实例化对象,创建对象,注意有括号的存在

    Student1 st=new Student1("John",19,"336 West Street","Computer");//下、实例化对象,创建对象,注意有括号的存在,赋值,实际参数

    pe.print();  //调用打印方法

    st.print(); //调用打印方法

    }

}

3.代码(super,this)

package 第四章;

class Person2 {//定义类

    protected String name;

    protected int age;

    public Person2(String na,int ag) {//定义方法

    name=na;

    age=ag;

    }

    public void olderoryounger(Person2 p) {//定义方法,比较年龄大小

    int d;

    d=this.age-p.age;//引用类中的方法,变量

    System.out.print(this.name +" is");

    if (d>0) //比较年龄大小输出

        System.out.println("older than "+p.name);

    else if (d==0)

    System.out.println("same as"+p.name);

    else

        System.out.println("younger than"+p.name);

    }

    public void print(){//定义方法

    System.out.println("The object of"+this.getClass().getName()+":"+name+","+age);

    }

    }

    class Student2 extends Person2{//定义类

    String address;

    String department;

    public Student2(String na,int ag,String ad,String de){//定义方法

    super (na,ag);

    address=ad;

    department=de;

    }

    public void print(){//定义方法

    super.print();//超类中的打印方法

    System.out.println("The other information of student:"+address+","+department);

    }

    }

    public class Student2_ex{//定义类

    public static void main(String arga[]) {//主函数,程序入口

    Person2 pe=new Person2("Tom",20);//创建对象

    Student2 st=new Student2("John",19, "336 Mest Street","Computer"); //创建对象

    pe.print();

    st.print();

    pe.olderoryounger(st);

    }

}

4,代码(最终类和抽象类)

package 第四章;

abstract class Shape {//定义一个抽象类,作为超类

abstract protected double area();//定义抽象的方法

abstract protected void girth();//定义抽象的空方法

}

class Rectangle extends Shape{ //定义类,子类,继承超类

float width,length;

Rectangle(float w,float l){//定义方法

width=w; length=l;

}

public double area(){//定义一个方法,覆盖父类方法

return width*length;

}

public void girth(){//定义一个空的方法,覆盖父类方法

   

};

}

public class Shape_ex{//定义主类

public static void main(String args[]){//主函数,程序入口

Rectangle rc=new Rectangle(6,12);//实例化对象

System.out.println("The area of rectangle :"+rc.area());//输出

}

}

5、知识点

【1】继承的基本概念

【2】子类的创建

【3】null,this,super对象运算符

【4】最终类和抽象类

六.类的多态性

1.代码(方法重载)

package 第四章;

public class Shapearea {//

    public double area (float r) { //方法

    return Math.PI*r*r;

    }

    public double area(float a,float b){ //方法重载

    return a*b;

    }

    public double area (float a,float b,float c){//方法重载

    float d;

    d=(a+b+c)/2;

    return Math.sqrt(d*(d-a)*(d-b)*(d-c));

    }

    public static void main(String args[]){//主函数

    Shapearea sh=new Shapearea();

    System.out.println("The area of circle:" +sh.area(3));

    System.out.println("The area of rectangle:"+sh.area(7,4));

    System.out.println("The area of triangle:"+sh.area(3,4,5));

    }

}

正在上传…重新上传取消

2.代码

package 第四章;

abstract class Shape{ //抽象类

    abstract protected double area();//持久性方法没有方法体

  }

    class Circle extends Shape{//子类

        float r;

        public Circle(float a) {

           r=a;

        }

    public double area(){

    System.out.print("Calculate the area of circle: ") ;

    return Math.PI*r*r;

    }

    }

    class Cylinder extends Circle{ //子类

    float h;

    public Cylinder(float a,float b) {

        super(a);

        h=b;

    }

    public double area(){ 

    System.out.print("Calculate the area ofcylinder: ");

    return 2*Math.PI*r*r+2*Math.PI*r*h;

    }

    }

    public class Shapecc_ex{//主类

    public static void main(String arg[]) {//主函数入口

    Circle cl=new Circle(3);//实例化

    Cylinder cd=new Cylinder(2,5);

  System.out.println(cl.area());

  System.out.println(cd.area());

    }

    }

3.代码

package 第四章;

public class Shapeccd_ex {

    public static void main(String arg[]){

    Circle c1=new Circle(3);

    Cylinder cd=new Cylinder(2,5);

    System.out.println(c1.area());

    System.out.println(cd.area());

    c1=cd;

    System.out.println(c1.area());

    }

}

4、知识点

【1】方法的重载

【2】方法的覆盖

【3】前期绑定和后期绑定