classBook{privateString title;privatedouble price;publicBook(Stringt,doublep){ title = t; price = p;} //setter、getter略publicStringgetInfo(){return"书名:"+ title +",价格:"+ price;}}publicclassMainClass{publicstaticvoidmain(String[]args){Book book =newBook("Java基础入门",89.8);System.out.println(book.getInfo());}}
public Book(String t, double p) {
title = t;
price = p;
}
public Book(String title, double price) {
title = title;
price = price;
}
class Book {
private String title;
private double price;
public Book(String title, double price) {
this.title = title;
this.price = price;
}
//setter、getter略
public String getInfo() {
return "书名:" + this.title + ",价格:" + this.price;
}
}
public class MainClass {
public static void main(String[] args) {
Book book = new Book("Java基础入门", 89.8);
System.out.println(book.getInfo());
}
}
class Book {
private String title;
private double price;
public Book(String title, double price) {
this.title = title;
this.price = price;
}
//setter、getter略
public void print() {
System.out.println("**********");
}
public String getInfo() {
this.print();
return "书名:" + this.title + ",价格:" + this.price;
}
}
public class MainClass {
public static void main(String[] args) {
Book book = new Book("Java基础入门", 89.8);
System.out.println(book.getInfo());
}
}
class Book {
private String title;
private double price;
public Book() {
System.out.println("一个新的Book类对象生成");
}
public Book(String title) {
System.out.println("一个新的Book类对象生成");
this.title = title;
}
public Book(String title, double price) {
System.out.println("一个新的Book类对象生成");
this.title = title;
this.price = price;
}
//setter、getter略
public String getInfo() {
return "书名:" + this.title + ",价格:" + this.price;
}
}
public class MainClass {
public static void main(String[] args) {
Book book = new Book("Java基础入门", 89.8);
System.out.println(book.getInfo());
}
}
class Book {
private String title;
private double price;
public Book() {
System.out.println("一个新的Book类对象生成");
}
public Book(String title) {
this();
this.title = title;
}
public Book(String title, double price) {
this(title);
this.price = price;
}
//setter、getter略
public String getInfo() {
return "书名:" + this.title + ",价格:" + this.price;
}
}
public class MainClass {
public static void main(String[] args) {
Book book = new Book("Java基础入门", 89.8);
System.out.println(book.getInfo());
}
}
class Book {
private String title;
private double price;
public Book() {
this("Java", 89.8);
System.out.println("一个新的Book类对象生成");
}
public Book(String title) {
this();
this.title = title;
}
public Book(String title, double price) {
this(title);
this.price = price;
}
//setter、getter略
public String getInfo() {
return "书名:" + this.title + ",价格:" + this.price;
}
}
public class MainClass {
public static void main(String[] args) {
Book book = new Book("Java基础入门", 89.8);
System.out.println(book.getInfo());
}
}
class Emp {
private int empno;
private String ename;
private double sal;
private String dept;
public Emp() {
this.empno = 0;
this.ename = "无雇员";
this.sal = 0.0;
this.dept = "未定";
}
public Emp(int empno) {
this.empno = empno;
this.ename = "临时工";
this.sal = 800.0;
this.dept = "后勤";
}
public Emp(int empno, String ename) {
this.empno = empno;
this.ename = ename;
this.sal = 2000.0;
this.dept = "技术部";
}
public Emp(int empno, String ename, double sal, String dept) {
this.empno = empno;
this.ename = ename;
this.sal = sal;
this.dept = dept;
}
//setter、getter略
public String getInfo() {
return "雇员编号:" + this.empno + ",名称:" + this.ename + ",工资:" + this.sal + ",部门:" + this.dept;
}
}
public class MainClass {
public static void main(String[] args) {
Emp ea = new Emp();
Emp eb = new Emp(9527);
Emp ec = new Emp(7566, "ALLEN");
Emp ed = new Emp(7869, "KING", 5000.0, "Boss");
System.out.println(ea.getInfo());
System.out.println(eb.getInfo());
System.out.println(ec.getInfo());
System.out.println(ed.getInfo());
}
}
class Emp {
private int empno;
private String ename;
private double sal;
private String dept;
public Emp() {
this(0, "无雇员", 0.0, "未定");
}
public Emp(int empno) {
this(empno, "临时工", 800.0, "后勤");
}
public Emp(int empno, String ename) {
this(empno, ename, 2000.0, "技术部");
}
public Emp(int empno, String ename, double sal, String dept) {
this.empno = empno;
this.ename = ename;
this.sal = sal;
this.dept = dept;
}
//setter、getter略
public String getInfo() {
return "雇员编号:" + this.empno + ",名称:" + this.ename + ",工资:" + this.sal + ",部门:" + this.dept;
}
}
public class MainClass {
public static void main(String[] args) {
Emp ea = new Emp();
Emp eb = new Emp(9527);
Emp ec = new Emp(7566, "ALLEN");
Emp ed = new Emp(7869, "KING", 5000.0, "Boss");
System.out.println(ea.getInfo());
System.out.println(eb.getInfo());
System.out.println(ec.getInfo());
System.out.println(ed.getInfo());
}
}
class Book {
public void print() {
//this就是当前调用方法的对象
System.out.println("this = " + this);
}
}
public class MainClass {
public static void main(String[] args) {
Book booka = new Book();
Book bookb = new Book();
System.out.println("booka = " + booka);
booka.print();
System.out.println("----------------------------");
System.out.println("bookb = " + bookb);
bookb.print();
}
}
class A {
private B b;
//2、执行A()
public A() {
//3、实例化B类对象
this.b = new B(this); //4、this == temp
this.b.get(); //7、调用b.get()
}
//10、调用print()
public void print() {
System.out.println("Hello World !");
}
}
class B {
private A a;
//5、调用B(A a),a == temp
public B(A a) {
this.a = a; //6、保存a对象(temp)
}
//8、调用this.a(temp).print()
public void get() {
this.a.print();
}
}
public class MainClass {
public static void main(String[] args) {
//1、实例化A类对象,调用A()
A temp = new A();
}
}