forked from DuGuQiuBai/Java
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b3c6d6
commit 1b4dd3f
Showing
44 changed files
with
1,077 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
class Phone { | ||
private String brand; | ||
private int price; | ||
private String color; | ||
|
||
public Phone() {} | ||
|
||
public Phone(String brand,int price,String color) { | ||
this.brand = brand; | ||
this.price = price; | ||
this.color = color; | ||
} | ||
|
||
public void setBrand(String brand) { | ||
this.brand = brand; | ||
} | ||
|
||
public String getBrand() { | ||
return brand; | ||
} | ||
|
||
public void setPrice(int price) { | ||
this.price = price; | ||
} | ||
|
||
public int getPrice() { | ||
return price; | ||
} | ||
|
||
public void setColor(String color) { | ||
this.color = color; | ||
} | ||
|
||
public String getColor() { | ||
return color; | ||
} | ||
|
||
public void show() { | ||
System.out.println("我的手机是:"+brand+",价格是:"+price+",颜色是:"+color); | ||
} | ||
} | ||
class PhoneTest { | ||
public static void main(String[] args) { | ||
//无参+setXxx() | ||
Phone p = new Phone(); | ||
p.setBrand("三星"); | ||
p.setPrice(1000); | ||
p.setColor("黑色"); | ||
p.show(); | ||
|
||
//带参 | ||
Phone pp = new Phone("华为",799,"白色"); | ||
pp.show(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
构造方法:给对象的数据进行初始化 | ||
特点: | ||
A:方法名与类名相同 | ||
B:没有返回值类型,连void都没有 | ||
C:没有具体的返回值 | ||
构造方法的格式: | ||
修饰符 类名(...) { | ||
} | ||
构造方法的注意事项: | ||
A:如果你不提供构造方法,系统会给出默认无参构造方法 | ||
B:如果你提供了构造方法,系统将不再提供默认无参构造方法 | ||
这个时候,如果你还想继续使用无参构造方法,只能自己给出。 | ||
推荐:永远自己给出无参构造方法。 | ||
C:构造方法也是可以重载的 | ||
D:构造方法中可以有return语句吗? | ||
可以。只不过是return; | ||
*/ | ||
class Student { | ||
//成员变量 | ||
private String name; | ||
private int age; | ||
|
||
//构造方法 | ||
public Student() { | ||
System.out.println("我是无参构造方法"); | ||
//return; | ||
} | ||
|
||
public Student(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Student(int age) { | ||
this.age = age; | ||
} | ||
|
||
public Student(String name,int age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
//getXxx()/setXxx()方法 | ||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setAge(int age) { | ||
this.age = age; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
//显示所有成员变量的方法 | ||
public void show() { | ||
System.out.println("姓名是:"+name+",年龄是:"+age); | ||
} | ||
} | ||
|
||
class StudentDemo { | ||
public static void main(String[] args) { | ||
//创建对象 | ||
Student s = new Student(); | ||
s.show(); | ||
|
||
//创建对象 | ||
Student s2 = new Student("林青霞"); | ||
s2.show(); | ||
|
||
//创建对象 | ||
Student s3 = new Student(28); | ||
s3.show(); | ||
|
||
//创建对象 | ||
Student s4 = new Student("林青霞",28); | ||
s4.show(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
类的组成: | ||
成员变量 | ||
构造方法 | ||
成员方法 | ||
给类的成员变量赋值有几种方式: | ||
A:setXxx()方法 | ||
B:带参构造方法 | ||
练习: | ||
Phone: | ||
成员变量:brand,price,color | ||
构造方法:无参,带参 | ||
成员方法:setXxx()/getXxx() | ||
show() | ||
PhoneTest: | ||
main | ||
*/ | ||
class Student { | ||
private String name; | ||
private int age; | ||
|
||
public Student() {} | ||
|
||
public Student(String name,int age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setAge(int age) { | ||
this.age = age; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public void show() { | ||
System.out.println("姓名是:"+name+",年龄是:"+age); | ||
} | ||
} | ||
|
||
class StudentTest { | ||
public static void main(String[] args) { | ||
//无参+setXxx | ||
Student s1 = new Student(); | ||
s1.setName("林青霞"); | ||
s1.setAge(28); | ||
System.out.println(s1.getName()+"---"+s1.getAge()); | ||
s1.show(); | ||
System.out.println("----------------------------"); | ||
|
||
//带参 | ||
Student s2 = new Student("王重阳",82); | ||
System.out.println(s2.getName()+"---"+s2.getAge()); | ||
s2.show(); | ||
|
||
} | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
定义一个类MyMath,提供基本的加减乘除功能,然后进行测试。 | ||
*/ | ||
class MyMath { | ||
public int add(int a,int b) { | ||
return a + b; | ||
} | ||
|
||
public int subtract(int a,int b) { | ||
return a - b; | ||
} | ||
|
||
public int multiply(int a,int b) { | ||
return a * b; | ||
} | ||
|
||
public int divide(int a,int b) { | ||
return a / b; | ||
} | ||
} | ||
class MyMathDemo { | ||
public static void main(String[] args) { | ||
//创建对象 | ||
MyMath my = new MyMath(); | ||
|
||
System.out.println("加法:"+my.add(23,34)); | ||
System.out.println("减法:"+my.subtract(23,34)); | ||
System.out.println("乘法:"+my.multiply(2,4)); | ||
System.out.println("除法:"+my.divide(10,4)); | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
定义一个员工类,自己分析出几个成员, | ||
然后给出成员变量,构造方法,getXxx()/setXxx()方法, | ||
以及一个显示所有成员信息的方法。并测试。 | ||
Employee: | ||
成员变量:员工编号,姓名,职位 | ||
构造方法:无参,带参 | ||
成员方法:getXxx()/setXxx()方法,show() | ||
*/ | ||
class Employee { | ||
private String eid; | ||
private String name; | ||
private String job; | ||
|
||
public Employee() {} | ||
|
||
public Employee(String eid,String name,String job) { | ||
this.eid = eid; | ||
this.name = name; | ||
this.job = job; | ||
} | ||
|
||
public void setEid(String eid) { | ||
this.eid = eid; | ||
} | ||
|
||
public String getEid() { | ||
return eid; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setJob(String job) { | ||
this.job = job; | ||
} | ||
|
||
public String getJob() { | ||
return job; | ||
} | ||
|
||
public void show() { | ||
System.out.println("员工编号是:"+eid+",姓名是:"+name+",职位是:"+job); | ||
} | ||
} | ||
|
||
class EmployeeDemo { | ||
public static void main(String[] args) { | ||
//无参 | ||
Employee e = new Employee(); | ||
e.setEid("itcast007"); | ||
e.setName("周星驰"); | ||
e.setJob("高级工程师"); | ||
e.show(); | ||
|
||
//带参 | ||
Employee e2 = new Employee("itcast003","刘德华","挖掘机工程师"); | ||
e2.show(); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
定义一个类Demo,其中定义一个求两个数据和的方法,定义一个测试了Test,进行测试。 | ||
什么时候定义成员变量呢? | ||
要想知道在类中什么时候定义成员变量,就应该先思考成员变量和类的关系? | ||
成员变量是描述类的基本信息的。 | ||
也就是说,只有和类有关系的变量才可以定义为成员变量。 | ||
*/ | ||
class Demo { | ||
private int x; | ||
private int y; | ||
|
||
public Demo() {} | ||
|
||
public Demo(int x,int y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
public void setX(int x) { | ||
this.x = x; | ||
} | ||
|
||
public int getX() { | ||
return x; | ||
} | ||
|
||
public void setY(int y) { | ||
this.y = y; | ||
} | ||
|
||
public int getY() { | ||
return y; | ||
} | ||
|
||
//成员变量已经有x,y了。这里就没有必要在定义了 | ||
/* | ||
public int sum(int x,int y) { | ||
return x + y; | ||
} | ||
*/ | ||
|
||
public int sum() { | ||
return x + y; | ||
} | ||
} | ||
|
||
class Test { | ||
public static void main(String[] args) { | ||
Demo d = new Demo(); | ||
d.setX(10); | ||
d.setY(20); | ||
int result = d.sum(); | ||
System.out.println(result); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
定义变量的时候,范围越小越好。 | ||
*/ | ||
class Demo { | ||
public int sum(int x,int y) { | ||
return x + y; | ||
} | ||
} | ||
|
||
class Test2 { | ||
public static void main(String[] args) { | ||
Demo d = new Demo(); | ||
int result = d.sum(10,20); | ||
System.out.println(result); | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Oops, something went wrong.