Introduction to Java object-oriented learning
1 class Person{ 2 //定义int 类型的变量 3 int age; 4 //定义speak()方法 5 void speak (){ 6 System.out.println(“***”) 7 } 8 } //Person 类名,age 是成员变量,speak()成员方法
Member variable class | Initial value | Member variable class | Initial value |
byte | 0 | double | 0.0D |
short | 0 | char | null character, '\u0000' |
int | 0 | boolean | false |
long | 0L | Reference data type | null |
float | 0.0F |
1 class Student{ 2 private String name; //将name属性私有化 3 private int age; //将age属性私有化 4 //下面是公有的getXXX()和setXXX()方法 5 public String getName (){ 6 return name; 7 } 8 public void setName(String stuName){ 9 name = stuName ; 10 } 11 public int getAge (){ 12 return age ; 13 } 14 public void setAge(int stuAge){ 15 //下面是对传入的参数进行检查 16 if(stuAge<=0){ 17 System.out.println("年龄不合法"); 18 }else { 19 age = stuAge ; //对属性赋值 20 } 21 } 22 public void introduce(){ 23 System.out.println("大家好,我叫"+name+",我今年"+age+"岁!"); 24 } 25 } 26 public class Example01{ 27 public static void main(String[] args){ 28 Student stu = new Student(); 29 stu.setAge(-30); 30 stu.setName("李芳"); 31 stu.introduce(); 32 } 33 }
②在方法名前面没有返回值类型的声明;
③在方法中不能使用return语句返回一个值;
1 class Person{ 2 //构造方法 3 public Person(){ 4 //无参构造方法 5 } 6 public Person(int age){ 7 age = a; //有参构造方法 8 } 9 public void speak(){ 10 System.out.println(“I am” +age+”years old !"); 11 } 12 } 13 public class Example{ 14 public static void main (String [] args){ 15 Person p = new Person(20); //实例化Person对象 16 p.speak(); 17 } 18 }
①通过this关键字可以明确地访问一个类的成员变量,解决与局部变量名称冲突问题。
1 class Person{ 2 int age ; 3 public Person(int age){ 4 this.age = age ; //访问成员变量 5 } 6 public int getAge(){ 7 return this .age; 8 } 9 }
②通过this关键字调用成员方法。
③构造方法是在实例化对象时被Java虚拟机自动调用的,在程序中不能像调用其他方法一样去调用构造方法,但可以在一个构造方法中使用“this(【参数1,参数2……】)”的形式来调用其他的构造方法。
①只能在构造方法中使用this 调用其他的构造方法,不能在成员方法中使用。
②在构造方法中,使用this 调用构造方法的语句必须位于第一行,且只能出现一次。
③不能在一个类的两个构造方法中使用this互相调用。
1 class Single{ 2 private static Single INSTANCE = new Single(); 3 private Single(){} 4 public static Single getInstance(){ 5 return INSTANCE ; 6 } 7 } 8 //上面单例又可写成以下形式 9 class Single{ 10 private Single(){} 11 public static final Single INSTANCE = new Single(); /*变量名INSTANCE的前面有三个修饰符,其中,public的作用是允许外部直接访问该变量,static 的作用是 让外部可以使用 “类名.变量名“的方式来访问变量,final的作用是禁止外部对该变量进行修改。*/ 12 } 13 14 class Example { 15 public static void main(String[] args){ 16 Single s = Single.getInstance(); // getInstance()方法是获得Single类实例对象的唯一途径,Single 类是一个单例的类 17 } 18 }
1 class Outer{ 2 private int num = 4; //定义类的成员变量 3 //下面的代码定义了一个成员方法,方法中访问内部类 4 public void test(){ 5 Inner inner = new Inner(); 6 inner.show(); 7 } 8 //下面的代码定义了一个成员内部类 9 class Inner{ 10 void show(){ 11 //在成员内部类的方法中访问外部类的成员变量 12 System.out.println("num = "+num); 13 } 14 } 15 } 16 public class Example16 { 17 public static void main(String[] args){ 18 Outer outer = new Outer(); //创建外部类对象 19 outer.test(); //调用test()方法 20 } 21 } 22 //直接创建内部类对象示例 23 public class Example16 { 24 public static void main(String[] args){ 25 Outer.Inner inner = new Outer().Inner() ; //创建内部类对象 26 inner.show(); //调用show()方法 27 } 28 } 29 //当内部类被声明为私有,外界将无法访问。
1 class Outer{ 2 private static int num = 6; //定义类的成员变量 3 //下面的代码定义了一个静态内部类 4 static class Inner{ 5 void show(){ 6 System.out.println("num = "+num); 7 } 8 } 9 } 10 public class Example16 { 11 public static void main(String[] args){ 12 Out.Inner inner = new Out.Inner(); //创建内部类对象 13 inner.show(); //调用内部类的方法 14 } 15 }
注意:①在静态内部类中只能访问外部类的静态成员。
The above is the detailed content of Introduction to Java object-oriented learning. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
