I want to add setName method to Person class in Java

Asked 2 years ago, Updated 2 years ago, 30 views

I just started programming, so I'm sorry if you're saying something strange.

I'd like to add a new setName method to the Person class (the getName and getAge methods are defined), but how can I do it without a return value?

java

2022-09-30 14:24

1 Answers

Person Class (getName and getAge methods are defined)

If is referring to the following conditions:

public class Person {

  private String name;
  private age;

  public String getName() {
    return name;
  }

  public int getAge(){
    return;
  }
}

The code is as follows:

public class Person {

  private String name;
  private age;

  public String getName() {
    return name;
  }

  public int getAge(){
    return;
  }

  // Additional parts from here

  public void setName(String name){
    this.name = name;
  }

  public void setAge(interage){
    This.age=age;
  }
}

void indicates no return value.


2022-09-30 14:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.