Object-Oriented Programming (OOP) in Java
Java is an object-oriented programming language, which means that it is designed to allow developers to create classes and objects that can be used to model real-world entities. Here is an example of a simple class in Java:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
In this example, we define a class called Person that has two instance variables (name and age) and four methods (Person, getName, getAge, setName, and setAge). The Person method is a constructor that takes two parameters (name and age) and initializes the instance variables. The getName and getAge methods return the values of the instance variables, while the setName and setAge methods set the values of the instance variables.
No comments:
Post a Comment