1.package
如果两个class不在同一个包中。
(1)使用包名+.
/*
定义customer zhanshan=new customer();之后,利用zhanshan.age访问customer里面的成员变量。
abs( )是对整型数据进行取绝对值的函数。
fabs( )是对float类型的数据取绝对值的函数。
*/
2.使用import语句
import day1.day1class;//具体的
import day1.*;//万能
2.声明格式不一样
类名+对象名=new+类名();
Hello hi=new Hello();
//Hello是类名,hi是对象名
3.定义输出区别
public class day2class {
private int money; //定义
private int getmoney()
{
return money;
}
public static void main(String []args)
{
day2class exa=new day2class();
exa.money=3000;
int m=exa.getmoney();
System.out.println("money="+m);//此输出必须有加号
//System.out.println("money="+exa.getmoney());//也可以
//System.out.println("money="+money);//即使把money换成public也不可以
}
}
4.Integer
int a=10;
Integer ta=new Integer(6);
System.out.println(a);//10
System.out.println(ta);//6
Integer tb=new Integer(9);
int c=ta+tb;
System.out.println(c);//15
int d=a+tb;
System.out.println(d);//19
**********************************************
int a=10;
Integer ta=new Integer(6);
a=ta;
System.out.println(a);//6
System.out.println(ta);//6
**********************************************
int a=10;
Integer ta=8;
//ta是一个对象,a是一个变量
Integer tb=ta.intValue();//对象之间的赋值 8
System.out.println(tb);
//Integer tb=ta; 是错误的,对象不能这样赋值
Integer tc=a;//把变量赋值给对象 10
System.out.println(tc);
4.大小写字母转换
(1)isLowerCase() 方法用于判断指定字符是否为小写字母,如果字符为小写,则返回 true;否则返回 false。toUpperCase()转化为大写字母。
(2.)isUpperCase() 方法用于判断指定字符是否为大写字母,
如果字符为大写,则返回 true;否则返回 false。
toLowerCase()转化为小写字母。
public class day2class{
public static void main(String[]args)
{
char a[]= {'a','B','c'};
for(int i=0;i<a.length;i++)
if(Character.isLowerCase(a[i]))
{
a[i]=Character.toUpperCase(a[i]);
}
else if (Character.isUpperCase(a[i]))
{
a[i]=Character.toLowerCase(a[i]);
}
for(int i=0;i<a.length;i++)
{System.out.println(" "+a[i]);
}
}
}
5.装箱
Character ch1=new Character('q'); √
Character ch2='q'; √装箱
将一个char类型的参数传递给需要一个Character类型参数的方法时,那么编译器会自动地将char类型参数转换为Character对象。 这种特征称为装箱,反过来称为拆箱。
// 原始字符 'a' 装箱到 Character 对象 ch 中
Character ch = 'a';
// 原始字符 'x' 用 test 方法装箱
// 返回拆箱的值到 'c'
char c = test('x');
6.反编译器
windows+R cmd
输入 javap java. util.Date
(java. util.Date)是类
7.
红色方块代表private
黄色菱形代表protected
绿色圆形代表public、
蓝色三角形代表友好的
8.
System.out.println("Hello World"); //输出Hello World并且换行
System.out.print("Hello World"); //输出Hello World不换行
Java中的println和printf的区别在于:
println是用于输出参数内容,然后换行,其参数个数固定为一个。
printf是用于输出带各种数据类型的占位符的参数,其参数个数是不定的。
9.继承
10. final
final关键字可以修饰类,成员变量和方法中的局部变量
final修饰类,就不能有子类了,这个就是最后了
final int m;初始化之后,m就是常量了
finalvoid setA()不能被重写
package day1 ;
//final class A error因为它有子类,不能用final
class A{
//final double PI;//没有初始化
final double PI=3.1415926;//PI 是常量
public double getArea(final double r){
//r=98 error,因为不允许再改变r的值
return PI*r*r;
}
public final void speak()
{
System.out.println("How is everything ? ");
}
}
class B extends A
{
/*error 不能重写speak
public void speak()
{
System.out.println("你好 ");
}
*/
}
11.上转型对象
假设 ,A是B的父类,当用子类创建一个对象,并把这个对象的引用放到父类的对象中时,例如
A a=new B();
或者
A a;
B b=new B();
a=b;
这时,称a是b的上转型对象。
“父类等于子类,父类是子类的上转型”
package day1;
class day1class1{
String name="zhangsan";
String say() {
return "我是父类";
}
}
class Son extends day1class1{
int age=5;
String say() {
return "我是子类";
}
}
public class day1class{//这个名必须与类名相同
public static void main(String args[]) {
Son s1=new Son();
System.out.println("s1.name"+s1.name);
System.out.println("s1.say"+s1.say());
day1class1 s2=new Son();//S2是上转型对象
System.out.println("s2.name : "+s2.name);
System.out.println("s2.say : "+s2.say());
Son s3=(Son) s2;//强制类型转换
//s3.
}
}
例子
//People.java 子类
public class People extends Anthropoid {
char m='A';
int n=60;
void computer(int a,int b)
{
int c=a+b;
System.out.println(a+"+"+b+"="+c);
}
//Anthropoid。Java 父类
public class Anthropoid {
double m=12.58;
void cry(String s)
{
System.out.println(s);
}
}
// day1class。java 主类
public class day1class{
public static void main(String args[]) {
People people=new People();
Anthropoid monkey=people;
monkey.cry("I love ");//调用父类
//monket.n=100; ERROR
//monley.computer(12,19); ERROR
System.out.println(monkey.m);
System.out.println(people.m);
People zhang=(People)monkey;
zhang.computer(55,22);
zhang.m='T';//强制转换之后调用
System.out.println(zhang.m);
}
}
//输出
I love
12.58
A
55+22=77
T
12.abstract
不能用new创建对象
abstract class A{
abstract int sum(int x,int y);
int sub(int x,int y) {
return x-y;
}
}
class B extends A{
int sum(int x,int y) {//子类必须重写sum的父类方法
return x+y;
}
}
public class day1class {
public static void main(String args[])
{
//A a=new A();
B b=new B();
int sum=b.sum(30, 20);
int sub=b.sub(30, 20);
System.out.println("sum "+sum);
System.out.println("sub "+sub);
}
}
- 接口 interface
implements
package day1;
interface 收费{
public void 收取费用();
}
interface 调节温度{
public void controltemperature();
}
class 公共汽车 implements 收费{
public void 收取费用() {
System.out.println("公共汽车:1元/张,不算公里数");
}
}
class 出租车 implements 收费 , 调节温度{
public void 收取费用 () {
System.out.println("出租车:3.20/公里,起价3公里");
}
public void controltemperature()
{
System.out.println("安装了HAIr空调");
}
}
class 电影院 implements 收费 , 调节温度{
public void 收取费用 () {
System.out.println("门票:十元一张");
}
public void controltemperature()
{
System.out.println("安装了中央空调空调");
}
}
public class day1class{
public static void main(String[]args)
{
公共汽车 七路=new 公共汽车();
出租车 天域=new 出租车();
电影院 红星= new 电影院();
天域.controltemperature();
红星.controltemperature();
}
}
输出
安装了HAIr空调
安装了中央空调空调
package day1;
interface Advertisement {
public void showAdvertisement();
public String getCorpName();
}
class PP implements Advertisement{
public void showAdvertisement() {
System.out.println("@@@@@@@@@@@@@");
System.out.println("没有最好,只有更好");
System.out.println("@@@@@@@@@@@@@");
}
public String getCorpName() {
return "飞利浦";
}
}
class Le implements Advertisement{
public void showAdvertisement() {
System.out.println("##############");
System.out.println("让世界变得更小些");
System.out.println("##############");
}
public String getCorpName() {
return "联想集团";
}
}
class AD {
public void show(Advertisement adver)
{
System.out.println("广告牌显示"+adver.getCorpName()+"噜噜噜");
adver.showAdvertisement();
}
}
public class day1class {
public static void main(String []args)
{
AD board=new AD();
board.show(new PP());
board.show(new Le());
}
}
输出
广告牌显示飞利浦噜噜噜
@@@@@@@@@@@@@
没有最好,只有更好
@@@@@@@@@@@@@
广告牌显示联想集团噜噜噜
##############
让世界变得更小些
##############
14内部类
package day1;
//import day1.redcowform.redcow;
class redcowform{
static String formname;
redcow cow;//内部类声明对象
redcowform(String s)
{
cow=new redcow(150,112,5000);
formname=s;
}
public void showcowmess()
{
cow.speak();
}
//内部类的声明
class redcow{
String cowname="红牛";
int height,weight,price;
redcow(int h,int w ,int p){
height=h;
weight=w;
price=p;
}
void speak() {
System.out.println("o偶是"+cowname+" ,生活在 "+formname);
}
}
}
public class day1class{
public static void main(String args[]) {
redcowform form =new redcowform("红牛农场");
form.showcowmess();
form.cow.speak();
}
}
注意最后一行
form.cow.speak();
15.隐藏类
abstract class speak{
public abstract void speakhello();
}
class student{
void f(speak sp) {
sp.speakhello();
}
}
public class day1class{
public static void main(String args[])
{
speak s=new speak() {
public void speakhello() {
System.out.println("大家好0");
}
};
s.speakhello();
student st=new student();
st.f(
new speak(){
public void speakhello() {
System.out.println("I am a student");
}
} );
}
}
注意这里
st.f( new speak()
{
public void speakhello()
{
System.out.println(“I am a student”);
}
} );