classes
This commit is contained in:
18
java/classes/Car.java
Normal file
18
java/classes/Car.java
Normal file
@ -0,0 +1,18 @@
|
||||
/**
|
||||
* this class is also declared with the public keyword to make it accessible
|
||||
* outside of the current file
|
||||
*/
|
||||
public class Car {
|
||||
public String model = "";
|
||||
public String manufacturer = "";
|
||||
|
||||
public void print_info() {
|
||||
System.out.println("Car Information:");
|
||||
/**
|
||||
* To access fields of the current object the this keyword is used. It is
|
||||
* possible to omit that keyword, but it's recommended to be used anyways.
|
||||
*/
|
||||
System.out.println("- manufacturer: " + this.manufacturer);
|
||||
System.out.println("- model: " + this.model);
|
||||
}
|
||||
}
|
5
java/classes/README.md
Normal file
5
java/classes/README.md
Normal file
@ -0,0 +1,5 @@
|
||||
# Java Example
|
||||
|
||||
Relevant Files:
|
||||
|
||||
- [Car.java](./Car.java)
|
18
java/classes/main.java
Normal file
18
java/classes/main.java
Normal file
@ -0,0 +1,18 @@
|
||||
public class main {
|
||||
public static void main(String[] args) {
|
||||
// create a new car and store it in the variable 'a'
|
||||
Car a = new Car();
|
||||
// set some data for that car
|
||||
a.manufacturer = "Benz";
|
||||
a.model = "Velo";
|
||||
|
||||
// do the same for a second car
|
||||
Car b = new Car();
|
||||
b.manufacturer = "Ford";
|
||||
b.model = "Model T";
|
||||
|
||||
// use a function of the car class to print out the information
|
||||
a.print_info();
|
||||
b.print_info();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user