java方法重载和重写

1、java的方法重载和重写,表示两种不同的类型。this关键字,出现在类的构造方法中,代表使用该构造方法所创建的对象。,this可以出现在实例方法中核构造方法中。但是不能出现在类方法中。实例方法只能通过对象来调用,不能通过类名来调用,当this关键字出现在实例方法中时,this就可以调用该方法的当前的对象。

public class ExampleMianJiJiSuan {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Circle circle=new Circle();
        Tixing lader=new Tixing(2,4,5);
        Student zhang=new Student();
        zhang.computerArea(circle);
        zhang.computerArea(lader);
        System.out.println("success");
    }
}
class Circle{
    double radius,area;
    void setRadius(double r){
        radius=r;
    }
    double getArea(){
        area=3.1415926*radius*radius;
        return area;
    }
    
}
class Tixing{
    double above,bottom,height;
    Tixing(double a,double b, double h){
        above=a;
        bottom=b;
        height=h;
    }
    double getArea(){
        return (above+bottom)*height/2;
    }
}
class Student{
    double computerArea(Circle c){
        double area=c.getArea();
        return area;
    }
    double computerArea(Tixing t){
        double area=t.getArea();
        return area;
    }

2、私有成员变量和方法在其他类中即使声明创建了该类的一个对象,在当前类中该对象也不能调用原本来中声明的私有变量和方法。

在一个源文件中编写命名的类总是在同一个包中,如果源文件使用import语句引入另一个包中的类,并用该类创建了一个对象,那么该类的这个对象将不能访问自己的友好变量,和友好方法。

原文地址:https://www.cnblogs.com/xinxianquan/p/8711610.html