1.类与对象
1.创建类
[修饰符] class 类名{//类的声明部分
[成员变量]
[成员方法]
}
public class School {
//成员变量
String name;
int jsN;
int jfN;
//成员方法
public void show(){
System.out.println("姓名"+name+jfN);
}
}
2.创建对象
类名 对象名 =new 类名();
#测试类
public class test2 {
public static void main(String[] args)
//创建对象
Girlf f= new Girlf();
//给成员变量赋值
f.name="急急急";
f.age=13;
//访问方法
f.eat();
System.out.println(f.name);
}
}
当创建对象后没有个成员变量赋值,每个类型的变量都有自己的默认值。
3.private
public class Girlf {
private int age;
// 对于每一个私有化的成员变量,都有提供get和set方法
//set方法:给成员变量赋值
//get方法:成员变量获取值
public void setAge(int age) {
if( age >= 18 && age <= 50){
this.age=age;
}else {
System.out.println("非法数据");
}
}
public int getAge(){
return age;
}
private String name;
public void setName(String name){
this.name=name;
}
public String getName() {
return name;
}
public void eat() {
System.out.println("吃饭");
}
public void drink() {
}
}
public class test2 {
public static void main(String[] args) {
Girlf f= new Girlf();
f.setAge(18);
f.setName("zsj");
f.eat();
System.out.println(f.getAge());
System.out.println(f.getName());
}
}
4.变量使用的就近原则
成员变量/局部变量
- 成员变量:声明在类中方法体之外、可以有缺省值、可以使用修饰符。作用域:整个类
- 局部变量:声明在方法体或代码块中、没有缺省值、只能使用final修饰。作用域:当前方法体
public class dirl {
public int age;
public void method(){
int age=10;
System.out.println(age); // 10
}
public static void main(String[] args) {
dirl d= new dirl();
d.age=11;
d.method();
}
}
5.this
this是一种特殊的引用,指向当前对象
如果发生局部变量和成员变量命名冲突时,可以通过this.成员变量名的方式区分成员变量和局部变量。
public class dirl {
public int age;
public void method(){
int age=10;
System.out.println(this.age); // 11 打印了成员变量
}
public static void main(String[] args) {
dirl d= new dirl();
d.age=11;
d.method();
}
}
6.static
在类中共享变量或者方法
package jie2;
public class Student {
private String name;
private int age;
static String teacher="zs";
public void study(){
System.out.println("study");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void show(){
System.out.println("show");
}
}
package jie2;
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student();
s1.setAge(18);
s1.setName("zs");
s1.teacher = "一号";
System.out.println(s1.getName() + "年龄:" + s1.getAge() + "," + s1.teacher);
Student s2 = new Student();
s2.setName("zj");
s2.setAge(12);
System.out.println(s1.getAge() + "年龄:" + s2.getAge()+ "," + s2.teacher);
}
}
7.构造方法
构造方法也叫构造器,构造函数。
作用:创建对象时候给成员进行赋值。
public class test {
private String username;
private String password;
private String email;
private String gender;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public test(String username, String password, String email, String gender, int age) {
this.username = username;
this.password = password;
this.email = email;
this.gender = gender;
this.age = age;
}
}
public static void main(String[] args) {
test t= new test("zsj","123456","12313@qq.com","男",18);
System.out.println(t.getAge());;
}
8.javabean
9.javaAPI
API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件的以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节。
通俗理解就是应用程序编程接口,是一些预先定义的类。
优点:可以直接使用这些已经被打包的类来做具体的应用,节约时间和精力,提高开发效率。