public class FatherClass {
}
public class SonClass extends FatherClass{
}
FatherClass father=new FatherClass();
if(father instanceof FatherClass) {
System.out.println("father是类FatherClass的对象");
}else {
System.out.println("father不是类FatherClass的对象");
}
SonClass son=new SonClass();
if(son instanceof SonClass) {
System.out.println("son是类SonClass的对象");
}else {
System.out.println("son不是类SonClass的对象");
}
if(son instanceof FatherClass) {
System.out.println("son是类FatherClass的对象");
}else {
System.out.println("son不是类FatherClass的对象");
}
public class FatherClass {
}
public class SonClass extends FatherClass{
}
FatherClass father=new FatherClass();
if(father.getClass().equals(FatherClass.class)) {
System.out.println("father是类FatherClass的对象");
}else {
System.out.println("father不是类FatherClass的对象");
}
SonClass son=new SonClass();
if(son.getClass().equals(SonClass.class)) {
System.out.println("son是类SonClass的对象");
}else {
System.out.println("son不是类SonClass的对象");
}
if(son.getClass().equals(FatherClass.class)) {
System.out.println("son是类FatherClass的对象");
}else {
System.out.println("son不是类FatherClass的对象");
}
package com.string;
import java.util.Scanner;
public class Character_Judge {
public static void main(String[] args) {
System.out.println("请随机输入一段字符串");
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
char s[] = str.toCharArray();
int char_num=0;//计算字母
int num = 0;//计算数字
int other = 0;//计算其他字符
for(int i=0;i<s.length;i++)
{
if (s[i]<='9'&&s[i]>='0')
num++;
else if (s[i]>='a'&&s[i]<='z'||s[i]>='A'&&s[i]<='Z')
char_num++;
else
other++;
}
System.out.println("字符有"+char_num+"个;数字有"+num+"个;其他字符有"+other+"个");
}
} <html>
<head>
<meta charset="utf-8" />
<title>显示转换</title>
</head>
<body>
<script type="text/javascript">
var priceOfApple = "3元";
var priceOfBanana = "3.5元";
priceOfApple = parseInt(priceOfApple);
var priceOfBanana2 = parseInt(priceOfBanana);
priceOfBanana = parseFloat(priceOfBanana);
if(priceOfApple == 3 && priceOfBanana2 == 3 && priceOfBanana == 3.5)
{
document.write(priceOfApple + priceOfBanana2 + priceOfBanana);
}
else
{
document.write("没有得到预期结果");
}
</script>
</body>
</html>例:将文本串中将李白的《静夜思》的各个部分分别提取出来,并格式化输出。标题加粗,文本居中对齐,诗歌正文颜色显示灰色
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<script type="text/javascript">
var comment = "静夜思李白床前明月光,疑是地上霜。举头望明月,低头思故乡。";
var partial = comment.substring(0,3); //取出标题
partial = partial.bold(); //标题加粗
document.write("<p align=\"center\">");//输出HTML标签"<p>",并设置居中对齐
docunment.write(partial); //输出标题
partial = comment.slice(3,5); //取出作者
document.write("<br />"); //输出换行标签<br>
document.write(partial); //输出作者
partial = comment.slice(5,17); //取出第一句诗文
partial = partial.fontcolor("gray"); //设置颜色为灰色
document.write("<br />");
document.write(partial);
partial = comment.slice(17,29); //取出第二就诗文
partial = partial.fontcolor("gray"); //设置颜色为灰色
document.write("<br />");
document.write(partial);
document.write("</p>");
</script>
</body>
</html>
基本数据类型
字符串型数据
在JavaScript中,字符串型数据是用引号括起来的文本字符串。
在JavaScript中不区分“字符”和“字符串”,字符也被当做字符串处理。在字符串中可以包括用于特殊目的的字符。
<script type="text/javascript">
var hello = "你好"; //双引号
var name = 'fanyoh'; //单引号
alert(hello + name);
</script>
1
2
3
4
5
数值型数据
JavaScript中用于表示数字的类型成为数字型,不像其他语言那样区分整型和浮点型。数字型用于双精度浮点值来表示数字数据,可以表示(-2^53 ~ +2^53)区间中的值。数字的值可以用普通记数法也可以使用科学记数法。表示方式:
10,10.1,0.1 表示数字
3e7,0.3e7 表示科学记数
1
2
复合数据类型
对象的概念
在面向对象的设计模式中,将数据和处理数据的方法捆绑在一起形成的整体称为对象。它封装了数据和操作数据的方法,使用时要先创建这个对象,用new运算符来调用对象的构造函数。
日期对象
JavaScript将与日期相关的所有特性封装进Date对象,主要用来进行一些与时间相关的操作,比如:获取当前系统时间,使用前要创建该对象的一个实例:
date = new Date(); //直接创建
date = new Date( val ); //指定日期创建
date = new Date(y , m , d[ , h, [ , min[ , sec[ , ms] ]]]); //指定日期创建
1
2
3
全局对象
全局对象是所有全局方法的拥有者,用来统一管理全局方法,全局方法也就是指全局函数。该对象不能使用new运算符来创建对象实例,所有的方法直接调用即可。
数学对象
数学对象(Math)封装了与数学相关的特性,包括一些常数和数学函数,主要使用一些简单的数学基本运算比如:正弦、余弦、正切、取绝对值等;(不需要用new关键字创建,直接使用即可)
字符串对象
String对象封装了与字符串有关的特性,主要用来处理字符串。通过String对象,可以对字符串进行剪切、合并、替换等等。可以调用该对象的构造函数创建一个实例,其实在定义一个字符串类型变量时也就创建了一个String对象实例。
调用String对象的方法或属性形式如“对象名.方法名”或“对象名.属性名”,构造函数如下:
String(strVal[]);
---------------------
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 建造者模式1
{
class ThinnerBuilder : PersonBuilder
{
public ThinnerBuilder(Graphics g,Pen p) : base(g, p) { }
public override void BuildArmLeft()
{
g.DrawLine(p, 60, 50, 40, 100);
}
public override void BuildArmRight()
{
g.DrawLine(p, 70, 50, 90, 100);
}
public override void BuildBody()
{
g.DrawRectangle(p, 60, 50, 10, 50);
}
public override void BuildHead()
{
g.DrawEllipse(p, 50, 20, 30, 30);
}
public override void BuildLegLeft()
{
g.DrawLine(p, 60, 100, 45, 150);
}
public override void BuildLegRight()
{
g.DrawLine(p, 70, 100, 85, 150);
}
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 建造者模式1
{
abstract class PersonBuilder
{
protected Graphics g;
protected Pen p;
public PersonBuilder(Graphics g, Pen p)
{
this.g = g;
this.p = p;
}
public abstract void BuildHead();
public abstract void BuildArmLeft();
public abstract void BuildArmRight();
public abstract void BuildBody();
public abstract void BuildLegLeft();
public abstract void BuildLegRight();
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 建造者模式1
{
abstract class PersonBuilder
{
protected Graphics g;
protected Pen p;
public PersonBuilder(Graphics g, Pen p)
{
this.g = g;
this.p = p;
}
public abstract void BuildHead();
public abstract void BuildArmLeft();
public abstract void BuildArmRight();
public abstract void BuildBody();
public abstract void BuildLegLeft();
public abstract void BuildLegRight();
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 建造者模式1
{
abstract class PersonBuilder
{
protected Graphics g;
protected Pen p;
public PersonBuilder(Graphics g, Pen p)
{
this.g = g;
this.p = p;
}
public abstract void BuildHead();
public abstract void BuildArmLeft();
public abstract void BuildArmRight();
public abstract void BuildBody();
public abstract void BuildLegLeft();
public abstract void BuildLegRight();
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 建造者模式1
{
class FatterBuilder:PersonBuilder
{
public FatterBuilder(Graphics g, Pen p) : base(g, p) { }
public override void BuildArmLeft()
{
g.DrawLine(p, 60, 50, 40, 100);
}
public override void BuildArmRight()
{
g.DrawLine(p, 70, 50, 90, 100);
}
public override void BuildBody()
{
g.DrawRectangle(p, 90, 70, 40, 70);
}
public override void BuildHead()
{
g.DrawEllipse(p, 50, 20, 30, 30);
}
public override void BuildLegLeft()
{
g.DrawLine(p, 60, 100, 45, 150);
}
public override void BuildLegRight()
{
g.DrawLine(p, 70, 100, 85, 150);
}
}
}