1:简单Java类
定义:
指的是可以描述某一类信息的程序类,例如:描述 一个人,描述一本书,描述一个部门,描述一个雇 员,并且类中没有特别复杂的逻辑操作,只作为一种 信息存储的媒介存在。
结构:
类名称一定要有意义,可以明确的描述某一类事物;
类之中的所有属性必须用private封装,同时封装后的属性必须有setter,getter方法;
类之中可以提供无数多个构造方法,但是必须要保留有无参构造的方法;
类之中不允许出现任何的输出语句,所有内容的获取必须返回;
【非必须】可以提供有一个获取对象详细信息的方法;
class Dept { // 类名称可以明确描述出某类事物
private long deptno ;
private String dname ;
private String loc ;
public Dept() {} // 必须提供有无参
public Dept(long deptno,String dname,String loc) {
this.deptno = deptno ;
this.dname = dname ;
this.loc = loc ;
}
public String getInfo() {
return "【部门信息】部门编号:" + this.deptno + "、部门名称:" + this.dname + "、部门位置:" + this.loc ;
}
public void setDeptno(long deptno) {
this.deptno = deptno ;
}
public void setDname(String dname) {
this.dname = dname ;
}
public void setLoc(String loc) {
this.loc = loc ;
}
public long getDeptno() {
return this.deptno ;
}
public String getDname() {
return this.dname ;
}
public String getLoc() {
return this.loc ;
}
}
public class JavaDemo {
public static void main(String args[]) {
Dept dept = new Dept(10,"技术部","北京") ;
System.out.println(dept.getInfo()) ;
}
}
2:static关键字
可以用来定义属性和方法;
2-1 static定义属性
一个类中,所有的属性一旦定义了实际上内容都交给由各自的堆内存空间所保存;
问题的出现
class Person { // 创建所有同一个国家的类
private String name ;
private int age ;
String country = "中华民国" ; // 国家,暂时不封装
public Person(String name,int age) {
this.name = name ;
this.age = age ;
}
// setter、getter略
public String getInfo() {
return "姓名:" + this.name + "、年龄:" + this.age + "、国家:" + this.country ;
}
}
public class JavaDemo {
public static void main(String args[]) {
Person perA = new Person("张三",10) ;
Person perB = new Person("李四",10) ;
Person perC = new Person("王五",11) ;
System.out.println(perA.getInfo()) ;
System.out.println(perB.getInfo()) ;
System.out.println(perC.getInfo()) ;
}
}
在产生很多(1000000)个对象后,country的属性需要修改,发现每一个对象拥有各自的属性,重复的属性修改不方便。
要解决这个问题,可以将country属性修改为公共属性,用static标注。
class Person { // 创建所有同一个国家的类
private String name ;
private int age ;
static String country = "中华民国" ; // 国家,暂时不封装
public Person(String name,int age) {
this.name = name ;
this.age = age ;
}
// setter、getter略
public String getInfo() {
return "姓名:" + this.name + "、年龄:" + this.age + "、国家:" + this.country ;
}
}
public class JavaDemo {
public static void main(String args[]) {
Person perA = new Person("张三",10) ;
Person perB = new Person("李四",10) ;
Person perC = new Person("王五",11) ;
perA.country = "中华人民共和国" ;
System.out.println(perA.getInfo()) ;
System.out.println(perB.getInfo()) ;
System.out.println(perC.getInfo()) ;
}
}
此时会发现对象中的所有属性都发生了改变,country是一个公共属性。
注意
static属性不受到类实例化对象的控制。static属性可以在没有实例化对象的时候使用(代码如下)。
public class Demo{
public static void main(String args[]){
System.out.println(Person,country);
Person.country = "中华人名共和国";
Person per = new Person("张三",10);
System.out.println(per.getInfo());
}
}
//考虑到公共信息时才会使用到static属性;
//非static属性必须在实例化对象产生之后才可以使用;
//而static属性可以在没有实例化对象产生的情况下直接通过类名称调用
2-2 static定义方法
特点:直接由类名称在没有实例化对象的情况下进行调用。
class Person { // 创建所有同一个国家的类
private String name ;
private int age ;
private static String country = "中华民国" ; // 国家
public Person(String name,int age) {
this.name = name ;
this.age = age ;
}
public static void setCountry(String c) { // static方法
country = c ;
}
// setter、getter略
public String getInfo() {
return "姓名:" + this.name + "、年龄:" + this.age + "、国家:" + this.country ;
}
}
public class JavaDemo {
public static void main(String args[]) {
Person.setCountry("中华人民共和国") ;
Person per = new Person("张三",10) ;
System.out.println(per.getInfo()) ;
}
}
总结
static方法只允许调用static属性或static方法;
非static方法允许调用static属性或者static方法;
所有的static定义的方法,属性。都可以在没有实例化对象的前提下使用;
所有的非static属性方法必须要有实例化对象的情况下才可以使用。
下面是两个应用举例
a:实现对实例化对象个数的统计。
class Book {
private String title ;
private static int count = 0 ;
public Book(String title) {
this.title = title ;
count ++ ;
System.out.println("第" + count + "本图书创建出来。") ;
}
}
public class JavaDemo {
public static void main(String args[]) {
new Book("Java") ; new Book("JSP") ; new Book("Spring") ;
}
}
b:实现属性的自动命名处理
规则:如果传递了title属性,就使用传递的属性内容,如果没有传递属性,则自动采用“NOTTITLE-编号”的形式命名。
class Book {
private String title ;
private static int count = 0 ;
public Book() {
this("NOTITLE - " + count ++) ;
}
public Book(String title) {
this.title = title ;
}
public String getTitle() {
return this.title ;
}
}
public class JavaDemo {
public static void main(String args[]) {
System.out.println(new Book("Java").getTitle()) ;
System.out.println(new Book("JSP").getTitle()) ;
System.out.println(new Book("Spring").getTitle()) ;
System.out.println(new Book().getTitle()) ;
System.out.println(new Book().getTitle()) ;
}
}