public class Student {
static String name;
private double score;
public Student(){};
public Student(double score) {
this.score = score;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
public class test {
public static void main(String[] args) {
Student.name="刘";
Student s1=new Student();
s1.name="马";
Student s2=new Student();
s2.name="秋";
System.out.println(Student.name);
}
}
//秋
static只有一个变量,所以修改别的类中的变量,所以的name都会变化。
比如系统的中统计用户的个数,在每次用户类中的构造函数中++。
public class User {
public static int num;//初始为0
public User(){
User.num+=1;
}
}
public class test {
public static void main(String[] args) {
User u1=new User();
User u2=new User();
User u3=new User();
User u4=new User();
User u5=new User();
User u6=new User();
System.out.println(u1.num);
}
}
//输出 6
public class LoginDemo {
public static void main(String[] args) {
String code="";
String data="qwertyuiopasdfghjklzxcvbnm";
Random r=new Random();
for(int i=0;i<4;++i){
int t=r.nextInt(data.length());
code+=data.charAt(t);
}
System.out.println(code);
}
}
public class RegisterDemo {
public static void main(String[] args) {
String code="";
String data="qwertyuiopasdfghjklzxcvbnm";
Random r=new Random();
for(int i=0;i<4;++i){
int t=r.nextInt(data.length());
code+=data.charAt(t);
}
System.out.println(code);
}
}
上述两个代码可以改成如下:
public class MyUtil {
public static String createCode(int n) {
String code="";
String data="qwertyuiopasdfghjklzxcvbnm";
Random r=new Random();
for(int i=0;i<n;++i){
int t=r.nextInt(data.length());
code+=data.charAt(t);
}
return code;
}
}
public class LoginDemo {
public static void main(String[] args) {
System.out.println(MyUtil.createCode(4));
}
}
public class RegisterDemo {
public static void main(String[] args) {
System.out.println(MyUtil.createCode(5));
}
}
这样就会简洁很多。
在MyUtil中加入private MyUtil(){};后,这样可以防止别的代码中创建MyUtil类。
public class Student {
static int num;
static {
System.out.println("静态代码块执行了");
}
}
public class Test {
public static void main(String[] args) {
System.out.println(Student.num);
System.out.println(Student.num);
}
}
//输出
//静态代码块执行了
//0
//0
静态代码块只有在创建的第一次会执行。一般用来对类变量进行初始化赋值。
public class Student {
public Student(){
System.out.println("无参构造函数执行了");
}
public Student(String name){
System.out.println("有参构造函数执行了");
}
{
System.out.println("实例代码块执行了");
}
}
public class Test {
public static void main(String[] args) {
Student stu=new Student();
}
}
//输出
实例代码块执行了
无参构造函数执行了
public class A {
private static A a=new A();
private A(){
}
public static A getObject(){
return a;
}
}
public class Test {
public static void main(String[] args) {
A a1=A.getObject();
A a2=A.getObject();
System.out.println(a1);
System.out.println(a2);
}
}
//输出
com.peng.test.single.A@4eec7777
com.peng.test.single.A@4eec7777
//懒汉式单例
public class B {
private static B b;
private B(){}
public static B getInstance(){
if(b==null){
b = new B();
}
return b;
}
}
public class Test {
public static void main(String[] args) {
B b1=B.getInstance();
B b2=B.getInstance();
System.out.println(b1);
System.out.println(b2);
}
}
//输出
com.peng.test.single.B@4eec7777
com.peng.test.single.B@4eec7777
继承不能继承多个,但是可以多层。
任何类都默认继承Object类,
public class A {
public int i;
public void printi(){
System.out.println("公共的可以继承");
}
private int j;
private void printj(){
System.out.println("私有的不可以继承");
}
}
public class B extends A {
public void printb(){
i=0;
System.out.println(i);
printi();
}
}
继承的应用
public class people {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class teacher {
private int salary;
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
public class student {
private String subject;
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
}
public class Test {
public static void main(String[] args) {
teacher t=new teacher();
t.setName("刘");
t.setSalary(100);
System.out.println(t.getName());
System.out.println(t.getSalary());
}
}
//输出
刘
100
重写的前面要加上@Override,便于理解。
继承变量中会就近原则。如果非要访问父类的变量,就用super来访问。
public class people {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void print_people(){
System.out.println("1111");
}
}
public class teacher extends people{
private int salary;
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public void print_people(){
System.out.println("66666");
}
}
public class Test {
public static void main(String[] args) {
teacher t=new teacher();
t.print_people();
}
}