类和对象
(1)类(class)
我们叫做class,可以看做是一个模版,或者图纸,系统根据类的定义来造出对象。
是抽象总结的结果
(2)对象
我们叫做Object,instance(实例)。以后我们说某个类的对象,某个类的实例。是一样的意思。
(3)总结
对象是具体的事物;类是对对象的抽象;
类可以看成一类对象的模板,对象可以看成该类的一个具体实例。
类是用于描述同一类型的对象的一个抽象概念,类中定义了这一类对象所应具有的共同的属性、方法。
(4)第一个类
示例:
// 每一个源文件必须有且只有一个public class,并且类名和文件名保持一致!public class Car {
}
class Tyre { // 一个Java文件可以同时定义多个class
}
class Engine {
}
class Seat {
}
示例:
public class Student {
//属性(成员变量)
int id;
String sname;
int age;
//方法
void study(){
System.out.println("我正在学习!");
}
//构造方法
Student(){
}
}
声明格式:
[修饰符] 类名(形参列表){
//n条语句
}
示例:
public class User {
int id; // id
String name; // 账户名
String pwd; // 密码
public User() {
}
public User(int id, String name) {
super();
this.id = id;
this.name = name;
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public static void main(String[] args) {
User u1 = new User();
User u2 = new User(101, "张三");
User u3 = new User(100, "李四", "123456");
}
}
示例: public class User { int id; //id String name; //账户名 String pwd; //密码 public User() { } public User(int id, String name) { System.out.println("正在初始化已经创建好的对象:"+this); this.id = id; //不写this,无法区分局部变量id和成员变量id this.name = name; } public void login(){ System.out.println(this.name+",要登录!"); //不写this效果一样 } public static void main(String[] args) { User u3 = new User(101,"张三"); System.out.println("打印张三对象:"+u3); u3.login(); } }
示例: public class TestThis { int a, b, c; TestThis() { System.out.println("正要初始化一个Hello对象"); } TestThis(int a, int b) { // TestThis(); //这样是无法调用构造方法的! this(); // 调用无参的构造方法,并且必须位于第一行! a = a;// 这里都是指的局部变量而不是成员变量// 这样就区分了成员变量和局部变量. 这种情况占了this使用情况大多数! this.a = a; this.b = b; } TestThis(int a, int b, int c) { this(a, b); // 调用带参的构造方法,并且必须位于第一行! this.c = c; } void sing() { } void eat() { this.sing(); // 调用本类中的sing(); System.out.println("你妈妈喊你回家吃饭!"); } public static void main(String[] args) { TestThis hi = new TestThis(2, 3); hi.eat(); } }
示例:
public class User2 {
int id; // id
String name; // 账户名
String pwd; // 密码
static String company = "京东"; // 公司名称
public User2(int id, String name) {
this.id = id;
this.name = name;
}
public void login() {
printCompany();
System.out.println(company);
System.out.println("登录:" + name);
}
public static void printCompany() {
// login();//调用非静态成员,编译就会报错
System.out.println(company);
}
public static void main(String[] args) {
User2 u = new User2(101, "张三");
User2.printCompany();
User2.company = "阿里";
User2.printCompany();
}
}
示例:
public class User3 {
int id; //id
String name; //账户名
String pwd; //密码
static String company; //公司名称
static {
System.out.println("执行类的初始化工作");
company = "东软";
printCompany();
}
public static void printCompany(){
System.out.println(company);
}
public static void main(String[] args) {
User3 u3 = new User3();
//User3 u3 = null;
}
}
示例:
public class User4 {
int id; //id
String name; //账户名
String pwd; //密码
public User4(int id, String name) {
this.id = id;
this.name = name;
}
public void testParameterTransfer01(User4 u){
u.name="高手";
}
public void testParameterTransfer02(User4 u){
u = new User4(200,"高手B");
}
public static void main(String[] args) {
User4 u1 = new User4(100, "高手A");
u1.testParameterTransfer01(u1);
System.out.println(u1.name);
u1.testParameterTransfer02(u1);
System.out.println(u1.name);
}
}
邀请人:玖叁叁
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。