Home > Enterprise >  How do you convert this python code to java?. How does one redefined static variables in java, speci
How do you convert this python code to java?. How does one redefined static variables in java, speci

Time:11-10

Python Code

class Person:
    def __init__(self, name, age, race):
        self.name = name
        self.age = age
        self.race = race
     
    def my_func(self):
        return "Hello my name is "   self.name

p1 = Person("Ray", 30, "white")  # it's already object

print(f'name = {p1.name}')  # output = Ray

print(f'age = {p1.age}')  # output = 30

print(f'race = {p1.race}') # output = white

print(p1.my_func())  # output = Hello my name is Ray

p1.age = 10  # edit object property

print(f'After update age = {p1.age}')

Java Code:

public class HelloWorld{

    public static void main(String[] args) {
    
    String name = " Ray";
    int age = 30;
    String race = "White";
            
        System.out.println("Hello my name is"  name);
        System.out.println(age);
        System.out.println(race);
     
   
    }
}

CodePudding user response:

Here is one way to do it:

public class HelloWorld {
    public record Person(String name, int age, String race) {
        public String myFunc() {
            return "Hello my name is "   name;
        }
    }

    public static void main(String[] args) {
        var p1 = new Person("Ray", 30, "white");

        System.out.format("name = %s%n", p1.name());

        System.out.format("age = %d%n", p1.age());

        System.out.format("race = %s%n", p1.race());

        System.out.println(p1.myFunc());

        p1 = new Person(p1.name(), 10, p1.race());

        System.out.format("After update age = %d%n", p1.age());
    }
}

You can also make Person a mutable class with getters and setters. See any tutorial on Java classes for examples. You can use Lombok to avoid the effort of writing out the getters and setters by hand.

CodePudding user response:

I'm probably an old fashion, but this is how I would write it:

public class Person {
    private String name;
    private int age;
    private String race;

    // java constant
    public static final int AGE = 30;

    // Constructor
    public Person(String name, int age, String race) {
        this.name = name;
        this.age = age;
        this.race = race;
    }

    // getters
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public int getRace() {
        return race;
    }

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

    public void setAge(int age) {
        this.age = age;
    }

    public void setRace(String race) {
        this.race = race;
    }

    public void myFunc() {
        System.out.println("Hello my name is "   name);
    }
}


public class Main {
    public static void main(String[] args) {
        Person person = new Person("Ray", Person.AGE, "white");

        System.out.println(person.getName()   ", "   person.getAge()   ", "   person.getRace());

        person.myFunc();

        person.setAge(10);
        System.out.println("After update age: "   person.getAge());
    }
}

EDIT: The AGE constant is just an example of defining a constant, I wouldn't normally set an age constant though.

CodePudding user response:

You can create a static class

static class Person {
    public int age = 10;

    public Person() {
    }
}

public static void main(String[] args) {
    // write your code here
    Person p = new Person();
    
    System.out.println(p.age);
    
    p.age = 5;

    System.out.println(p.age);

}

This will output:

100
5

I am not sure if that is what you mean however. Person class does not have to be static and you can also have a constructor that gets the properties.

  • Related