标签:java
class Student { private String name = "Tom"; private int age = 18; public String getName() { return name; } public void setName(String name) { if (name != null && !name.equals("")) { this.name = name; // 姓名不能为空 } } public int getAge() { return age; } public void setAge(int age) { if (age > 0) { this.age = age; // 年龄非负 } } } public class StudentDriver { public static void main(String[] args) { Student stu = new Student(); stu.setAge(-6); stu.setName(""); System.out.println("姓名:" + stu.getName() + " 年龄:" + stu.getAge()); } }
class Student { private String name = "Tom"; private int age = 18; //其他代码 }<span style="white-space:pre"></span>
class AddThing { int a, b = 12; //实例变量 public int add() { int total = a + b; //局部变量 return total; } }
class Foo{ public void go(){ int x; int z = x + 3; //无法通过编译,因为在使用之前没有值 } }
<span style="white-space:pre"> </span>int a = 3; byte b = 3; String str = a==b?"相等":"不等"; System.out.println(str);
标签:java
原文地址:http://blog.csdn.net/u010102951/article/details/42193051