局部变量:成员方法内的变量,方法执行结束则变量失效
静态变量、静态常量、静态方法,共享、省内存。调用格式:类名﹒静态成员
规定:静态方法中不可以使用this关键字、不可以调用非静态方法、不可以声明变量为static
权限修饰符:当成员变量修饰范围>类的修饰符范围时,权限服从于类
举例,自定义图书类
在Book包中创建Book类、Test类
Book类中:
package Book; public class Book { private String title; private String author; private double price; public Book(String title,String author,double price) { this.title=title;//将形参title赋值给成员变量title(同名) this.author=author; this.price=price; } public String getTitle() {//定义方法 return title; } public String getAuthor() { return author; } public double getPrice() { return price; } }
Test类中:
package Book; public class Test{ public static void main(String[] args) { Book book=new Book("《Java learning》","jv",50.8);//调用Book类中方法 System.out.println("Title is "+book.getTitle()); System.out.println("Author is "+book.getAuthor()); System.out.println("Price is "+book.getPrice()); } }