JAVA · ОСНОВЫ · LESSON 21
The subject can act.
Method of object · about 14 minutes
Learning with progressRead the summary for free. To save the work, log in to the account.
Type the code yourself
Type an example manually. Check the signs and brackets.
public class Main {
public static void main(String[] args) {
Cat cat = new Cat();
cat.name = "Рыжик";
cat.speak();
}
}
class Cat {
String name;
void speak() {
System.out.println(name + " мяу");
}
}Open the editor for class workSolve it yourself
In the Dog class, make the name field and the speak() method, which outputs name and « гав». Create a dog called «Бим» and call speak.
The answer is hidden. Use similar examples from the lesson notes.
Opening up your homework