您的位置:首页 > 编程语言 > Java开发

尚硅谷程序-java 封装登陆程序 1-2static 的一些相关应用 2-2代码块 3-1final关键字

2014-07-26 13:54 555 查看
1-1

java 封装登陆程序
class Login{
privateString name;
privateString password;
public Login(String name,String password){
this.name=name;
this.password=password;
}
publicboolean validate(){
if(name.equals("abc")&&password.equals("123")){
return true;
}else{
return false;
}
}
}
class Operate{
privateString args[];
public Operate (String args[]){
this.args=args;
if (args.length!=2){
System.out.println("输入参数不正确!!");
System.exit(1);
}
}
publicString getInfo(){
if(new Login(args[0],args[1]).validate()){
return "登陆成功";
}else{
return "登陆失败";
}
}
}
public class Text2 {
public static void main(String args[]){
System.out.println(new Operate(args).getInfo());
}
}

1-2

static 的一些相关应用

1.使用Static 可以统计一个类产生了多少个实例对象

.因为static 属性是所有对象共享的

class Persons {

privateString name;

private staticint count;

public Persons(){

count++;

System.out.println("产生了"+count+"个实例化对象");

}

publicString getInfor(){

return "姓名" +this.name;

}

}

public class Text2 {

public static void main(String args[]){

new Persons();

new Persons();

new Persons();

new Persons();

}

}

可以用Static 属性来自动命名。

class Persons {

privateString name;

private staticint count;

public Persons(){

count++;

this.name="Home"+count;

}

public Persons(String name){

this.name=name;

}

public void getInfor(){

System.out.println("姓名"+this.name);

}

}

public class Text2 {

public static void main(String args[]){

new Persons().getInfor();

new Persons().getInfor();

new Persons().getInfor();

new Persons("A").getInfor();

new Persons("B").getInfor();

new Persons("C").getInfor();

}

}

姓名Home1

姓名Home2

姓名Home3

姓名A

姓名B

姓名C

2-1

String 类的基本概念

两种实例化的区别;

一个字符串就是一个匿名对象

String的内容一旦声明则无法改变

String 类的相关操作方法

引用数据类型

this关键字

this调用属性

this调用方法

this调用构造方法

this调用构造方法,只能放在构造方法的首行

this表示当前对象,当前正在操作类中的对象

对象比较的操作

static关键字的使用

声明的属性是全局属性,可以由类名直接调用,

static方法不能访问非static类型的属性和方法

主方法的组成

程序的设计思路

对象数组

构造放发的私有化

作业

使用java的引用完成以下的代码开发

NODE->NODE->NODE

如果此种关系表示成功之后,现在要求完成一个可以动态增加节点的操作类,此类可以有以下的功能

增加节点,在节点后增加

查找节点,判断指定节点是否存在,节点中通过节点名称判断

删除节点

输出全部节点

2-2

代码块

在java中使用““{}”括起来的带吗交代码块,根据代码块的定义位置和声明的关键字的不同,

代码块可以分为

普通块

构造块

静态块

同步代码块

public class Text3 {

public static void main(String[]args){

{//普通代码块

int x=10;

System.out.println("x="+x);

}

int x=100;

System.out.println("x="+x);

}

}

x=10

x=100

构造块

class Demo{

{

System.out.println("*******构造块********");

}

public Demo(){

System.out.println("####构造方法#####");

}

}

public class Text3 {

public static void main(String[]args){

new Demo();

new Demo();

new Demo();

new Demo();

}

}

*******构造块********

####构造方法#####

*******构造块********

####构造方法#####

*******构造块********

####构造方法#####

*******构造块********

####构造方法#####

可以发现构造块会重复调用多次

构造快会优先于构造方法执行

静态块

使用static关键字声明代码块,称为静态代码块,为经态度属性初始化

class Demo{

{

System.out.println("*******构造块********");

}

static {

System.out.println("!!!!!类中的静态代码块!!!!!");

}

public Demo(){

System.out.println("####构造方法#####");

}

}

public class Text3 {

static {

System.out.println("!!!!!主类中的静态代码块!!!!!");

}

public static void main(String[]args){

new Demo();

new Demo();

new Demo();

new Demo();

}

}

!!!!!主类中的静态代码块!!!!!

!!!!!类中的静态代码块!!!!!

*******构造块********

####构造方法#####

*******构造块********

####构造方法#####

*******构造块********

####构造方法#####

*******构造块********

####构造方法#####

静态块优先于主方法执行而且只执行一次

思考在屏幕上输出Helloword

public class Text3 {

static {

System.out.println(">>>>M<<<<<");

System.exit(1);

}

public static void main(String[]args){

}

}

>>>>M<<<<<

在静态块中加入退出操作

3-1final关键字

声明类:声明的类不能有子类

声明方法:声明的方法不能被子类所覆写

声明的变量:声明的变量称为常量

final class Demo{

}

public class Text3 extends Demo {

public static void main(String[]args){

System.out.println("qq");

}

}

class Demo{
public final void fun(){
}
}
public class java extends Demo {
public void fun(){}
public static void main(String[]args){
System.out.println("qq");
}
}
class Demo{
public finalString NAME="aa";
public final void fun(){
NAME="vv";
}
}
public class java extends Demo {
public static void main(String[]args){
System.out.println("qq");
}
fina修饰的常量为大写
全局常量:使用public static final声明的常量为全局常量

4-1
String类
String的两种实例化方式
String本身是一个类,在String类中定义了构造方法
publicString(String original)
可以接收一个String对象,并重新实例化
还可以直接赋值

public class Text3 {
public static void main(String[]args){
String str1="hello ";
String str2=newString("word");
System.out.println(str1+str2);
}
}
hello word

4-2
String的比较方式
public class Text3 {
public static void main(String[]args){
String str1="hello";
String str2=newString("hello");
String str3=str1;
System.out.println("str1==str2 "+str1==str2);//>.<
System.out.println("str1==str3 "+(str1==str3));
System.out.println("str2==str3 "+(str3==str2));

System.out.println("str1==str2"+str1.equals(str2));
System.out.println("str1==str3"+str1.equals(str3));
System.out.println("str2==str3"+str3.equals(str2));
/**/
}
}
false
str1==str3 true
str2==str3 false
str1==str2true
str1==str3true
str2==str3true
字符串的类比较
public class Text3 {
public static void main(String[]args){
String str1="hello";
System.out.println("hello".equals(str1));//>.<
}
}
true
String的两种实例化方式
String str1="hello"

String str1=newString ("hello");

public class Text3 {
public static void main(String[]args){
String str1="hello";
String str2="hello";
String str3="hello";
System.out.println("str1==str2 "+(str1==str2));
System.out.println("str1==str2 "+(str2==str3));
System.out.println("str1==str2 "+(str1==str3));
}
}
str1==str2 true
str1==str2 true
str1==str2 true
实际上,以上的三个对象表示的是同一个引用,因为对于String来讲使用直接赋值的方式会在
字符池中保存内容,如果之后再声明字符串的时候发现内容一致,则不会开辟空间,而是从内存池中取出数据继续使用

字符串的内容一旦声明后不能改变
public class Text3 {
public static void main(String[]args){
String str1="hello";
str1+=" word";
System.out.println(str1);
}
}
hello word
字符串改变的是地址的指向

public class Text3 {
public static void main(String[]args){
String str1="hello";
for(int i=0;i<100;i++){
str1+=i;
}
System.out.println(str1);
}
}
这对程序性能低

4-3String的常用操作方法



char
charAt(int index)


返回指定索引处的
char
值。
char[]
toCharArray()


将此字符串转换为一个新的字符数组。
String(char[] value)


分配一个新的
String
,使其表示字符数组参数中当前包含的字符序列。
String(char[] value,int offset,int count)


分配一个新的
String
,它包含取自字符数组参数一个子数组的字符。
public class Text3 {
public static void main(String[]args){
char ch="hello".charAt(2);
char ch2[]="Hello java !!!".toCharArray();
System.out.println(ch);
for(char a:ch2){
System.out.print(a+",");
}
System.out.println();
String ss=newString (ch2,2,4);
System.out.println(ss);
}
}
l
H,e,l,l,o, ,j,a,v,a, ,!,!,!,
llo

byte[]
getBytes()


使用平台的默认字符集将此
String
编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
String(byte[] bytes)


通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的
String
String(byte[] bytes,int offset,int length)


通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的
String
public class Text3 {
public static void main(String[]args){
String str="hello word !!@";
byte b[]=str.getBytes();
String str1=newString (b);
String str2=newString (b,0,5);
System.out.println(str1);
System.out.println(str2);
}
}
hello word !!@
hello

判断是否以制定的字符串开头

boolean
startsWith(String prefix)


测试此字符串是否以指定的前缀开始。
boolean
endsWith(String suffix)


测试此字符串是否以指定的后缀结束。
public class Text3 {
public static void main(String[]args){
String str="##hello word !!@**";
System.out.println(str.startsWith("##"));
System.out.println(str.endsWith("**"));
}
}
true
true
替换操作

String
replaceAll(String regex,String replacement)


使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
public class Text3 {
public static void main(String[]args){
String str="##hello word !!@**";
String str1=str.replaceAll("l","s" );
System.out.println(str);
System.out.println(str1);
}
}
##hello word !!@**
##hesso word !!@**
字符串截取操作
全部截取

String
substring(int beginIndex)


返回一个新的字符串,它是此字符串的一个子字符串。
部分截取

String
substring(int beginIndex,int endIndex)


返回一个新字符串,它是此字符串的一个子字符串。
public class Text3 {
public static void main(String[]args){
String str="##hello word !!@**";
String sub1=str.substring(7);
String sub2=str.substring(0, 7);
System.out.println(sub1);
System.out.println(sub2);
}
}
word !!@**
##hello
字符串拆分操作

String[]
split(String regex)


根据给定正则表达式的匹配拆分此字符串。
public class Text3 {
public static void main(String[]args){
String str="##hello word !!@**";
String str1[]=str.split(" ");
for(String s:str1){
System.out.println(s);
}
}
}
##hello
word
!!@**
字符串查找操作
在字符串中查找是否存在制定的内容,可以使用以下的两个方法

int
indexOf(String str)


返回指定子字符串在此字符串中第一次出现处的索引。
String
concat(String str)


将指定字符串连接到此字符串的结尾。
public class Text3 {
public static void main(String[]args){
String str="##hello word !!@**";
System.out.println(str.contains("hello"));
System.out.println(str.indexOf("word"));
}
}
true
8

int
indexOf(String str,int fromIndex)


返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
public class Text3 {
public static void main(String[]args){
String str="##hello word !!@**";
System.out.println(str.contains("hello"));
if((str.indexOf("word",9))!=-1){
System.out.println("Ok"+str.indexOf("word",9));
}else{
System.out.println("No");
}

}
}
true
No
去掉字符串左右空格操作

String
trim()


返回字符串的副本,忽略前导空白和尾部空白。
取得字符串长度操作

int
length()


返回此字符串的长度。
字符串转大写操作

String
toUpperCase()


使用默认语言环境的规则将此
String
中的所有字符都转换为大写。
字符串转小写操作

String
toLowerCase(Locale locale)


使用给定
Locale
的规则将此
String
中的所有字符都转换为小写。
public class Text3 {
public static void main(String[]args){
String str="##hello word !!@**";
System.out.println(str.trim());
System.out.println(str.trim().toUpperCase());
System.out.println(str.trim().toLowerCase());
System.out.println(str.trim().length());
}
}
##HELLO WORD !!@**
##hello word !!@**
18
字符串题目
TOM89|JIM78|SYONE98
TOM->89
JIM->78
SYONE->98
public class Text3 {
public static void main(String[]args){
String str="TOM:89|JIM:78|SYONE:98";
String str1[]=str.split("\\|");
for(String s:str1){
String str2[]=s.split(":");
{
System.out.println(str2[0]+"-->"+str2[1]);
}
}
}
}
TOM-->89
JIM-->78
SYONE-->98

public class java {
public static void main(String[]args){
if(!validate(args[0])){
System.out.println("不对");
}else
System.out.println("嗯!!");
}
public staticboolean validate(String email){
boolean flag=true;
if((email.indexOf("@",(email.indexOf("@")+1)))!=-1||(email.indexOf(".",(email.indexOf
(".")+1)))!=-1){
flag=false;
System.out.println((email.indexOf("@",(email.indexOf("@")+1))));
System.out.println((email.indexOf(".",(email.indexOf(".")+1))));
}
if(flag){
if(!(email.indexOf("@")<email.indexOf("."))){
flag=false;
}
}
return flag;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: