Skip to content

Commit

Permalink
Java第十天
Browse files Browse the repository at this point in the history
  • Loading branch information
chao420456 committed Nov 25, 2015
1 parent 601c98b commit 5318c3d
Show file tree
Hide file tree
Showing 31 changed files with 908 additions and 0 deletions.
23 changes: 23 additions & 0 deletions day10/code/01_形式参数问题/ArgsDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//形式参数如果是基本类型,只需要传递该基本类型的值即可。
class Demo {
public int sum(int a,int b) {
return a + b;
}
}

class ArgsDemo {
public static void main(String[] args) {
Demo d = new Demo();

//变量
int x = 10;
int y = 20;
int z = d.sum(x,y);

//常量
int a = d.sum(10,20);

System.out.println(z);
System.out.println(a);
}
}
76 changes: 76 additions & 0 deletions day10/code/01_形式参数问题/ArgsDemo2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//形式参数如果是引用类型:
//具体类:该类的对象。
//抽象类:该类的子类对象。
//接口:该接口的实现类对象。
//数组:数组的地址值。其实就是一个数组对象。

/*
class Student {
public void study() {
System.out.println("好好学习,天天向上");
}
}
class StudentDemo {
public void method(Student s) {
s.study();
}
}
class ArgsDemo2 {
public static void main(String[] args) {
StudentDemo sd = new StudentDemo();
Student s = new Student();
sd.method(s);
}
}
*/

/*
abstract class Person {
public abstract void study();
}
class PersonDemo {
public void method(Person p) {
p.study();
}
}
class Student extends Person {
public void study() {
System.out.println("好好学习,天天向上");
}
}
class ArgsDemo2 {
public static void main(String[] args) {
PersonDemo pd = new PersonDemo();
Person p = new Student();
pd.method(p);
}
}
*/
interface Person {
public abstract void study();
}

class PersonDemo {
public void method(Person p) {
p.study();
}
}

class Student implements Person {
public void study() {
System.out.println("好好学习,天天向上");
}
}

class ArgsDemo2 {
public static void main(String[] args) {
PersonDemo pd = new PersonDemo();
Person p = new Student();
pd.method(p);
}
}
13 changes: 13 additions & 0 deletions day10/code/02_返回值问题/ReturnDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//如果返回值是一个基本数据类型,那么,返回的就是该基本类型的值。
class Demo {
public int sum(int a,int b) {
return a + b;
}
}

class ReturnDemo {
public static void main(String[] args) {
int x = new Demo().sum(10,20);
System.out.println(x);
}
}
59 changes: 59 additions & 0 deletions day10/code/02_返回值问题/ReturnDemo2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//如果返回值是一个引用数据类型
//类:返回的是该类的对象
//抽象类:返回的是该抽象类的子类对象
//接口:返回的是该接口的实现类对象
/*
class Student {
public void study() {
System.out.println("好好学习,天天向上");
}
}
class StudentDemo {
public Student getStudent() {
//Student s = new Student();
//return s;
return new Student();
}
public int sum(int a,int b) {
return a + b;
}
}
class ReturnDemo2 {
public static void main(String[] args) {
StudentDemo sd = new StudentDemo();
Student s = sd.getStudent(); //new Student();
s.study();
}
}
*/

/*
abstract class Person {
public abstract void study();
}
class PersonDemo {
public Person getPerson() {
return new Student();
}
}
class Student extends Person {
public void study() {
System.out.println("好好学习,天天向上");
}
}
class ReturnDemo2 {
public static void main(String[] args) {
PersonDemo pd = new PersonDemo();
Person p = pd.getPerson(); //new Student();
p.study();
}
}
*/

27 changes: 27 additions & 0 deletions day10/code/02_返回值问题/ReturnDemo3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//返回值类型是接口,其实返回的是接口的实现类对象
interface Person {
public abstract void study();
}

class PersonDemo {
public Person getPerson() {
return new Student();
}
}

class Student implements Person {
public void study() {
System.out.println("好好学习,天天向上");
}
}

class ReturnDemo3 {
public static void main(String[] args) {
//PersonDemo pd = new PersonDemo();
//Person p = pd.getPerson();
//p.study();

//链式编程
new PersonDemo().getPerson().study();
}
}
7 changes: 7 additions & 0 deletions day10/code/03_包/Demo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.liuyi;

public class Demo {
public int sum(int a,int b) {
return a + b;
}
}
65 changes: 65 additions & 0 deletions day10/code/03_包/HelloWorld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
包:其实就是文件夹
作用:对类进行分类管理
包的划分:
举例:
学生的增加,删除,修改,查询
老师的增加,删除,修改,查询
工人的增加,删除,修改,查询
A:按照功能分
cn.itcast.add
AddStudent
AddTeacher
AddWorker
cn.itcast.delete
DeleteStudent
DeleteTeacher
DeleteWorker
cn.itcast.update
cn.itcast.find
B:按照模块分
cn.itcast.student
add
delete
update
find
cn.itcast.teacher
add
delete
update
find
cn.itcast.worker
add
delete
update
find
定义包的格式:
package 包名;
带包的编译和运行:
手动式:
A:编译带包的类文件
B:自己手动创建包
C:把编译生成的class文件放到包里面去
D:运行即可,注意要带包名
java cn.itcast.HelloWorld
自动式:
A:编译的时候带一个参数 -d
javac -d . HelloWorld.java
B:运行即可,注意要带包名
java cn.itcast.HelloWorld
注意事项:
package语句必须是程序的第一条可执行的代码
package语句在一个java文件中只能有一个
如果没有package,默认表示无包名
*/
package cn.itcast;

class HelloWorld {
public static void main(String[] args) {
System.out.println("HelloWorld");
}
}
Binary file added day10/code/03_包/Test.class
Binary file not shown.
34 changes: 34 additions & 0 deletions day10/code/03_包/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cn.itcast;

/*
导包格式
import 包名..类名;
package,import,class有没有顺序关系(面试题)
有。
package --> import --> class
唯一 多个 多个
*/
import com.liuyi.Demo;

public class Test {
public static void main(String[] args) {
/*
com.liuyi.Demo d = new com.liuyi.Demo();
int result = d.sum(10,20);
System.out.println(result);
com.liuyi.Demo d2 = new com.liuyi.Demo();
int result2 = d2.sum(10,20);
System.out.println(result2);
com.liuyi.Demo d3 = new com.liuyi.Demo();
int result3 = d3.sum(10,20);
System.out.println(result3);
*/

Demo d = new Demo();
int result = d.sum(10,20);
System.out.println(result);
}
}
Binary file added day10/code/03_包/cn/itcast/Test.class
Binary file not shown.
Binary file added day10/code/03_包/com/liuyi/Demo.class
Binary file not shown.
35 changes: 35 additions & 0 deletions day10/code/04_权限修饰符/Father.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
权限修饰符:
本类 同一个包下的子类或其他类 不同包下的子类 不同包下其他类
private Y
默认 Y Y
protected Y Y Y
public Y Y Y Y
*/
package cn.itcast;

public class Father {
private void show() {
System.out.println("show");
}

void show2() {
System.out.println("show2");
}

protected void show3() {
System.out.println("show3");
}

public void show4() {
System.out.println("show4");
}

public static void main(String[] args) {
Father f = new Father();
f.show();
f.show2();
f.show3();
f.show4();
}
}
17 changes: 17 additions & 0 deletions day10/code/04_权限修饰符/Son.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cn.itcast;

class Son extends Father {
public static void main(String[] args) {
Father f = new Father();
//f.show();
f.show2();
f.show3();
f.show4();

Son s = new Son();
//s.show();
s.show2();
s.show3();
s.show4();
}
}
19 changes: 19 additions & 0 deletions day10/code/04_权限修饰符/Son2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.liuyi;

import cn.itcast.Father;

class Son2 extends Father {
public static void main(String[] args) {
Father f = new Father();
//f.show();
//f.show2();
//f.show3();
f.show4();

Son2 s = new Son2();
//s.show();
//s.show2();
s.show3();
s.show4();
}
}
Loading

0 comments on commit 5318c3d

Please sign in to comment.