java里this和set_Java中构造函数,getset和this的用法?

publicclassAbstract{publicstaticvoidmain(String[]args){Employeeemployee=newEmployee("张三",100);employee.job();}}abstractclassCompany{privateStringname;privatedoublemoney...

public class Abstract {

public static void main(String[] args) {

Employee employee = new Employee("张三", 100);

employee.job();

}

}

abstract class Company {

private String name;

private double money;

public Company(String name, double money) {

this.name = name;

this.money = money;

}

public abstract void job();

public void setName(String name) {

this.name = name;

}

public String getName() {

return name;

}

public void setMoney(double money) {

this.money = money;

}

public double getMonry() {

return money;

}

}

class Employee extends Company {

public Employee(String name, double money) {

super(name, money);

}

public void job() {

System.out.println(super.getName() + super.getMonry() + "敲代码");

}

}

class Manager extends Company {

private double bonus;

public Manager(String name, double money, double bonus) {

super(name, money);

this.bonus = bonus;

}

public void job() {

System.out.println(super.getName() + super.getMonry() + " "

+ this.bonus + "指挥敲代码");

}

}

class Clear extends Company {

public Clear(String name, double money) {

super(name, money);

}

public void job() {

System.out.println(super.getName() + super.getMonry() + "清理垃圾");

}

}

在new子类的时候将 参数 传入子类的构造函数里,再通过super() 传给父类构造函数,然后下面name和money是怎么在父类里面传递的,是怎么通过子类的覆盖父类的方法job() {super.getName() + super.getMonry()}最后打印输出的?

父类的构造方法里的this.name和setName里的this.name,各自的作用是什么,到底谁给谁啊?

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是 Java 代码示例: ```java public class Rectangle { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getArea() { return length * width; } public double getPerimeter() { return 2 * (length + width); } } public class Circle { private double radius; private static final double PI = 3.14159; public Circle(double radius) { this.radius = radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public double getArea() { return PI * radius * radius; } public double getPerimeter() { return 2 * PI * radius; } } ``` 在上面的代码,`Rectangle` 和 `Circle` 分别表示矩形和圆,都有相应的成员变量和方法。其,`Rectangle` 的 `length` 和 `width` 使用了 get 和 set 方法,这是体现静态成员变量的一种方式。`Circle` 的 `PI` 是一个静态常量,这是体现静态成员变量的另一种方式。`Rectangle` 和 `Circle` 都有构造函数,其 `Rectangle` 构造函数使用了传入参数并进行赋值,`Circle` 构造函数同样使用了传入参数并进行赋值。`Rectangle` 和 `Circle` 都有计算面积和周长的方法 `getArea` 和 `getPerimeter`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值